2013-02-13 55 views
0

我有這個型號:Django管理的變更表外鍵從froreignKey排序

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

從活動變更列表我要顯示與Project.name列。是否有可能顯示內容並使此列可排序

回答

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)