I want to display different forms in change_form depending on some model field’s values.
For example (imagine we have a realty agency):
if item.type == 'sale', I want to display ‘price’ field
if item.type == 'rent', I want to display ‘price_per_month’, ‘price_per_month’ etc – but not price
ps: example is not perfect – because its problem can be solved via database structure – but my question is about interface
There’s a
ModelAdminmethodget_formwhich takes the request and the object (watch out, it will beNonewhen you’re adding a new instance) and returns the form class.The usual warning about overriding undocumented methods applies, be careful when upgrading as Django’s api stability promise does not apply.