开发者

Changing field dynamically in Django's Admin panel

开发者 https://www.devze.com 2023-03-13 17:28 出处:网络
I have a Model with 2 CharFields : type and category. Both have choices with few possible values. For example: type can get \"Article\" or \"Video\" and category can get \"Extreme\", \"Funny\" or \"Am

I have a Model with 2 CharFields : type and category. Both have choices with few possible values. For example: type can get "Article" or "Video" and category can get "Extreme", "Funny" or "Amazing".

When an admin needs to add an entry, I want to first show him a list with the possible choices of types. Then, if he chooses "Article" I would like to show him another list开发者_高级运维 box which only contains "Extreme" or "Amazing". If he chooses "Video" I want to show him "Funny" or "Amazing".

The idea is to change the list box of category dynamically, based on the user's chice of the desired type.

Is that possible?

Meir


Sure, that's possible. But you have to do it in Javascript. Serverside python code can't do that.


(a) You must use javascript on the client side to do that on the page

(b) You should therefore also write custom validation to ensure that a valid combination is selected. Your ModelAdmin subclass should have a member form, which should be a custom ModelForm with its own implementation of clean.

0

精彩评论

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

关注公众号