I'm using Django 2.2 with Python 3.7, my question have two parts:
- The main goal, is to create dynamic fields over models on admin page, so for example, I have a
CharFieldwith multiple choices, and only thatcharfieldis being show with anullvalue, so if I change it to some option anothercharfieldwill appear below with more options to choose, of course, all thecharfieldsare defined inmodels.py, just hidden from the admin site until the condition is met, I think the best approach will be use JavaScript, I added the/media/folder to my project but I didn't understand well how to integrate the JavaScript on the admin site, any help over that? - I'm a beginner on JavaScript, I don't even know how to access the models attributes to use the
.show()and.hide()methods, can anyone point me into the right direction to establish simple if conditions to show or to hide the fields based on another one?
My admin class that holds the model I want to hide and show fields is the following one:
class RecipesAdmin(admin.ModelAdmin):
fieldsets = [
("Title/Description", {"fields": ["recipe_title", "recipe_ingredients"]}),
("Ingredients sequence", {"fields": ["sequence"]}),
("Recipe Sequence", {"fields": ["motor1", "motor2", "motor3", "motor4", "pump", "temp", "recipe_interval"]}),
("Photo and Create Date", {"fields": ["recipe_photo", "recipe_created"]})
]
formfield_overrides = {
models.TextField: {'widget': TinyMCE()}
}
If you see the "Recipe sequence" it has multiple values, I want motor2, 3, 4, and the rest to be appearing according the previous one, so no clue how to access this part of the code from JavaScript