可以 they are just Django models that happen to have a one-to-one link with a User model. As such, they do not get auto created when a user is created, but a could be used to create or update related models as appropriate.
这样来做
django中,如果一个数据库中的表之间有外键的话可以方便的通过一个表查询到其相关表的数据。如有下面三个model:
class Blog(models.Model):
name = models.CharField(max_length=100)
tagline = models.TextField()
def __unicode__(self):
return self.name
class Author(models.Model):
name = models.CharField(max_length=50)
email = models.EmailField()
def __unicode__(self):
return self.name
class Entry(models.Model):
blog = models.ForeignKey(Blog)
headline = models.CharField(max_length=255)
body_text = models.TextField()
pub_date = models.DateTimeField()
authors = models.ManyToManyField(Author)
n_comments = models.IntegerField()
n_pingbacks = models.IntegerField()
rating = models.IntegerField()
def __unicode__(self):
return self.headline
可以使用__来查询相关连的表里的数据,如:
Entry.objects.filter(blog__name__exact=‘Beatles Blog‘)
Blog.objects.filter(entry__headline__contains=‘Lennon‘)
class Blog(models.Model):
name = models.CharField(max_length=100)
tagline = models.TextField()
def __unicode__(self):
return self.name
class Author(models.Model):
name = models.CharField(max_length=50)
email = models.EmailField()
def __unicode__(self):
return self.name
class Entry(models.Model):
blog = models.ForeignKey(Blog)
headline = models.CharField(max_length=255)
body_text = models.TextField()
pub_date = models.DateTimeField()
authors = models.ManyToManyField(Author)
n_comments = models.IntegerField()
n_pingbacks = models.IntegerField()
rating = models.IntegerField()
def __unicode__(self):
return self.headline
可以使用__来查询相关连的表里的数据,如:
Entry.objects.filter(blog__name__exact=‘Beatles Blog‘)
Blog.objects.filter(entry__headline__contains=‘Lennon‘)
来源: