F
F
funkynick892020-05-19 18:45:50
Django
funkynick89, 2020-05-19 18:45:50

How to hide/show fields in Django admin depending on the selection of another field?

env:

Django 2.2
Python 3.7


Hello acquaintances! There are several models (simplified):

1) class Worker, whose fields
are -name -specialty
(ForeignKey to the “specialty” model)
-is_official conditional boolean field

2) class Specialty (specialty)
-position -shift
time

3) class HourRate (hourly rate)
-specialty (ForeignKey to the "specialty" model)
-size of the official rate (number)
-size of the unofficial rate
-start period
-end

period for the position "Operator", let's say 100 rubles, and in the next period already 120, etc.)
Now a separate template has been made for editing an employee's card (templates/admin/app/worker_change_form.html) and a separate JS script has been made to catch the event (for the time being it is tied to on.change).

The question is this:

To hide/show the required fields, depending on the selection in a certain field:
  • -Will it be necessary to create a separate view in order to pass the context with the fields of other models (speciality, hourrate) there and use them in this template?
  • -how is it better/easier to perform the required functionality? (when you select the position "Operator" and check the box in the IS_OFFICIAL field, the value of the official rate should be displayed, in the absence of the same, respectively, vice versa)
  • -In the future, with the customization of the admin panel, is it better to edit the standard template or connect jet-django?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question