【问题标题】:Show columns of referenced table instead of the object in django admin在 django admin 中显示引用表的列而不是对象
【发布时间】:2017-01-15 16:09:59
【问题描述】:

如何显示图书的 idtitleyear 列而不是“Books 对象”?

此屏幕截图显示了当前状态:

我的 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)

【问题讨论】:

    标签: django django-models


    【解决方案1】:

    在你的models.py 文件中,你可以在你关注的类中使用特殊的__str__ 方法来使你的对象更具描述性。 (这通常是大多数程序员这样做的方式)

    def __str__(self):
        return self.title #if your using 'title' as the attribute to identify your objects
    

    您可以选择任何其他属性来使您的对象具有描述性。

    祝你好运!

    【讨论】:

    • 我认为这是使用“str”而不是使用unicode的更常见的方式,但可能两者都可以。
    【解决方案2】:

    在每个模型的 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
    

    【讨论】:

    • 感谢它对我的帮助,我已将def __unicode__(self): return self.title 添加到class Books(models.Model),现在显示了 更改为普通字段并添加列 year
    • 简单地将年份添加到 unicode 函数的返回值中,如下所示:`return '{} - {}'.format(self.title, self.year)`。我现在意识到我没有回答你的问题。对不起。不幸的是,我不知道如何呈现您想要的列。
    • 谢谢,也许其他人有想法。
    【解决方案3】:

    Unicode 对我不起作用,但覆盖 str 有效

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-04-28
      • 1970-01-01
      • 2010-12-03
      • 2022-11-29
      • 2023-03-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多