0

I have this models:

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')

in the changelist from Activity I want to show a column with the Project.name. Is it possible to show the content and make this column sortable?

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 に答える