Answer the question
In order to leave comments, you need to log in
Dynamic object edit form in Django admin?
Let's say I have a model (in fact, the model is much more complicated using mptt polymorphic tree):
class MyModel(models.Model):
CHOICES = (
(1, 'Type1'),
(2, 'Type2')
)
my_type = models.Integerfield(choices=CHOICES)
field1 =
field2 =
field3 =
field4 =
....
Answer the question
In order to leave comments, you need to log in
class MyModelAdmin(admin.ModelAdmin):
my_filter = {1: ('field1', 'field2'), 2: ('field3', 'field4')}
def get_fields(self, request, obj=None):
fields = super().get_fields(request, obj)
if obj is None:
return fields
return [f for f in fields if f not in self.my_filter[obj.my_type]]
class MyModelAdmin(admin.ModelAdmin):
class Media:
js = (
'js/my_fields_filter.js',
)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question