以django例子里最常见的Books和Author为例,假设有Book和Author两张表。
models.py
- ...
- class Author(models.Model):
- name = models.CharField(_('Author Name'),max_length=128)
- def __unicode__(self):
- return self.name
- class Books(models.Model):
- title = models.CharField(_('Books'),max_length=128)
- author = models.Foreignkey(Author, related_name='book_author')
- def __unicode__(self):
- return self.title
- ...
如上述代码所示,假设每本书有一个作者,且名字唯一,然后我们对作者进行列表处理。
阅读全文…