开发者

Django - Dynamicly set the contents of list_display (admin view)

开发者 https://www.devze.com 2023-03-30 06:01 出处:网络
Is there a way to reset the contents of the list_display (in the admin view) for some model each time the model is filtered? (so that filtering by different objects will give a diffrent number of colu

Is there a way to reset the contents of the list_display (in the admin view) for some model each time the model is filtered? (so that filtering by different objects will give a diffrent number of columns in the display table).


For example:

I have three model in my django application: Child, Family, Pet. Child has a foregin key to Family, and Pet also has a foregin key to Family.

Now, in the admin view of Child, I want to be able to filter by Family, and after the filtering, the table will disp开发者_如何学Clay the child's name and the name of each one of the pets that his family owns. That way the number of columns in the displayed table changes according to the number of pets of the Family object the filtering was done by.

Thanks.


list_display is set in admin.py, and requires a server restart to become effective. As a result, it cannot be modified on the fly.

Additionally, Django does not seem to support the display of ManyToMany fields in this manner, so you cannot display the pet names in the list view "out of the box." According to the documentation:

If you want to do this nonetheless, give your model a custom method, and add that method's name to list_display.

0

精彩评论

暂无评论...
验证码 换一张
取 消