N
N
Nikita Sychev2016-02-15 19:58:23
Django
Nikita Sychev, 2016-02-15 19:58:23

How to create a field that appears depending on other fields?

I have some model and a model_type field described like this:

model_type = models.CharView(maxlength = 2, choice = [
  ('ST', 'Standard'),
  ('UN', 'Unlimited'),
  ('CU', 'Custom')
])

I need to do the following: if the Standard option is selected, then I need to show the start_time and end_time fields (both DateTimeField), if Unlimited is selected, then no fields are needed at all, and if Custom is selected, then the start_time, end_time (DateTimeField) and duration fields are needed ( DurationField).
How can this be done (when selecting one model_type in the admin panel, unnecessary fields should disappear)?
There was an idea to make one abstract model and three child ones, but then there will be three different sections in the admin panel. If anyone knows how to combine them into one section, then I think it would be better to do just that.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Stepan Krapivin, 2016-04-28
@xevin

It can be done not through an abstract model, but through proxy models.
Each proxy model will have its own form with the required fields and a default value for model_type (in other words, one proxy model for each model_type)
But it's easier to add js to the admin page that hides certain fields when changing model_type

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question