I need help with the following problem:
Using the Django admin, I would like to hide some fields in inline depending on whether the object exists.
Example equivalent to admin.ModelAdmin:
class ClassAdmin(admin.ModelAdmin):
...
def get_form(self, request, obj=None, **kwargs):
# if inline has not been saved
if obj is None:
self.fieldsets[0][1]['fields'] = tuple(x for x in self.fieldsets[0][2]['fields'] if (x!='field1'))
else:
self.inlines = self.inlines + [ClassInline,]
if obj.field1 == 'N':
self.fieldsets[2][7]['fields'] = tuple(x for x in self.fieldsets[2][8]['fields'] if (x!='field10'))
return super(ClassAdmin, self).get_form(request, obj, **kwargs)
How can I make it equivalent to an inline?
class ClassInline(admin.StackedInline):
# if obj:
# display filed1, field2
# else:
# display filed3, field4
I tried hard and not found something to help me solve the problem. Some topics I found:
Can someone show an example of code that can do the job?