我的模型设置如下:
class ParentModel(models.Model):
some_col = models.IntegerField()
some_other = models.CharField()
class ChildModel(models.Model)
parent = models.ForeignKey(ParentModel, related_name='children')
class ToyModel(models.Model)
child_owner = models.ForeignKey(ChildModel, related_name='toys')
现在在我的管理面板中,当我打开更改列表时,ParentModel
我希望 list_display 中有一个新字段/列,其中包含一个链接以打开更改列表,ChildModel
但应用过滤器仅显示来自所选父级的子级。现在我用这种方法实现了它,但我认为有一种更清洁的方法可以做到这一点,我只是不知道如何:
class ParentAdmin(admin.ModelAdmin)
list_display = ('id', 'some_col', 'some_other', 'list_children')
def list_children(self, obj):
url = urlresolvers.reverse('admin:appname_childmodel_changelist')
return '<a href="{0}?parent__id__exact={1}">List children</a>'.format(url, obj.id)
list_children.allow_tags = True
list_children.short_description = 'Children'
admin.site.register(Parent, ParentAdmin)
所以我的问题是,如果没有这种“链接黑客”,是否有可能实现同样的目标?ParentModel
如果它的任何孩子有玩具,是否可以在更改列表的单独列中指出?