Django: list_display( ) ForeignKey
现在我可以使用list_display()来显示列单词,并在使用click_数据模型时单击_times。代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | # 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()来显示列单词,并在使用用户数据模型(如下)时单击_times。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | # 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') |
号
我想展示的不仅是
试试这个:
1 2 3 4 5 6 7 8 9 10 11 12 13 | 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' |