我希望在管理面板中为一个测试程序创建一个视图,该程序记录书籍、出版商和作者(如 djangoproject.com)
我定义了以下两个模型。
class Author(models.Model):
first_name = models.CharField(max_length=30)
last_name = models.CharField(max_length=30)
email = models.EmailField()
def __unicode__(self):
return u'%s %s' % (self.first_name, self.last_name)
class Book(models.Model):
title = models.CharField(max_length=100)
authors = models.ManyToManyField(Author)
publisher = models.ForeignKey(Publisher)
publication_date = models.DateField()
def __unicode__(self):
return self.title
我想要做的是更改 Book 模型以引用任何作者的名字,并使用 admin.AdminModels 来显示它。
#Here is the admin model I've created.
class BookAdmin(admin.ModelAdmin):
list_display = ('title', 'publisher', 'publication_date') # Author would in here
list_filter = ('publication_date',)
date_hierarchy = 'publication_date'
ordering = ('-publication_date',)
fields = ('title', 'authors', 'publisher', 'publication_date')
filter_horizontal = ('authors',)
raw_id_fields = ('publisher',)
据我了解,同一模型中不能有两个外键。谁能给我一个如何做到这一点的例子?
我尝试过很多不同的事情,这让我一整天都发疯。我对 Python/Django 还很陌生。
需要明确的是 - 我只是希望作者的名字/姓氏出现在书名和出版商名称旁边。
Thanks