0

我有这个模型:

class Project(models.Model):
    name = models.CharField()

class Task(models.Model):
    title = models.CharField()
    project = models.ForeignKey('Project')

class Activity(models.Model):
    title = models.CharField()
    task = models.ForeignKey('Task')

在 Activity 的更改列表中,我想显示一个带有 Project.name 的列。是否可以显示内容并使该列可排序

4

1 回答 1

1
class ActivityAdmin(admin.ModelAdmin):
    list_display = [
        'title',
        'task',
        'project',
    ]

    def project(self, obj):
        return obj.task.project.name

    project.admin_order_field = 'task__project__name'

admin.site.register(Activity, ActivityAdmin)
于 2013-02-14T03:14:06.200 回答