我怎样才能显示列ID,标题和书,而不是“丛书对象”的一年?
该截图显示当前状态:
我model.py看起来是这样的:
from __future__ import unicode_literals
from django.db import models
class Authors(models.Model):
name = models.CharField(max_length=45, blank=True, null=True)
birthday = models.DateField(blank=True, null=True)
class Meta:
managed = True
db_table = 'authors'
class AuthorsBooks(models.Model):
author_id = models.OneToOneField('Authors', models.DO_NOTHING, db_column='author_id', primary_key=True)
book_id = models.OneToOneField('Books', models.DO_NOTHING, db_column='book_id', primary_key=True)
class Meta:
managed = True
db_table = 'authors_books'
unique_together = (('author_id', 'book_id'),)
class Awards(models.Model):
author = models.OneToOneField('Authors', models.DO_NOTHING, db_column='author', primary_key=True)
award_name = models.CharField(max_length=45)
year = models.IntegerField(blank=True, null=True)
class Meta:
managed = True
db_table = 'awards'
unique_together = (('author', 'award_name'),)
class Books(models.Model):
titel = models.CharField(max_length=45, blank=True, null=True)
year = models.IntegerField(blank=True, null=True)
class Meta:
managed = True
db_table = 'books'
在类AuthorsBooks我已经改变了两个外键OneToOneFields。
我的admin.py如下所示:
from django.contrib import admin
from myapp.models import Authors
...
class AwardsInline(admin.TabularInline):
model = Awards
class AuthorsBooksInline(admin.TabularInline):
model = AuthorsBooks
class AuthorsAdmin(admin.ModelAdmin):
list_display = ("name", "birthday" )
inlines = (AwardsInline, AuthorsBooksInline)
admin.site.register(Authors, AuthorsAdmin)
在每个模型的models.py添加一个Unicode功能。
class Authors(models.Model):
name = models.CharField(max_length=45, blank=True, null=True)
birthday = models.DateField(blank=True, null=True)
class Meta:
managed = True
db_table = 'authors'
def __unicode__(self):
return self.name
在你的models.py文件,你可以用你的关注类特殊__str__
方法,使你的对象更多的描述。 (这通常是像大多数程序员做到这一点)
def __str__(self):
return self.title #if your using 'title' as the attribute to identify your objects
你可以选择任何其他属性,使你的对象描述。
祝好运!