现在,我可以使用list_display()在使用Click_Data模型时显示 word 列和 click_times 。代码如下:
# models.py
class Click_Data(models.Model):
word = models.CharField(max_length=255)
click_times = models.IntegerField(default=0)
class Meta:
ordering = ('word',)
def __unicode__(self):
return self.word
# admin.py
class Click_DataAdmin(admin.ModelAdmin):
fieldsets = [
('Word', {'fields': ['word']}),
('Number', {'fields': ['click_times']}),
]
list_display = ('word', 'click_times')
但我不知道如何使用list_display()在使用 User_Data模型时显示 word 列和 click_times (其中)在下面)。
# models.py
class User_Data(models.Model):
user = models.ForeignKey(User)
click_data = models.ForeignKey(Click_Data)
class Meta:
ordering = ('user',)
def __unicode__(self):
return unicode(self.user)
# admin.py
class User_DataAdmin(admin.ModelAdmin):
fieldsets = [
('User', {'fields': ['user']}),
('Word', {'fields': ['click_data']}),
]
list_display = ('user', 'click_data')
我想在'click_data'
中不仅显示'字',还要'click_times',我怎么能实现这一目标?
答案 0 :(得分:3)
试试这个:
class User_DataAdmin(admin.ModelAdmin):
...
list_display = (..., 'get_click_times', 'get_word')
def get_click_times(self, obj):
return obj.click_data.click_times
get_click_times.short_description = 'Click Times'
def get_word(self, obj):
return obj.click_data.word
get_word.short_description = 'Word'