7
我已經建立了這樣的模式:Django管理變更列表過濾/鏈接到其他車型
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
更改列表的單獨列中指明其子女是否有玩具?
謝謝你的回答,這確實對我幫助很大。我很驚訝,django沒有更好的更改鏈接功能。無論如何謝謝,你的幫助現在解決了我所有的問題。 – 2011-12-18 13:44:04