django - How do I dynamically exclude a non-model field from the admin? -
I have added a custom field to a model farm for use in admin:
Category MyModel (models.Model): name = models.CharField (max_length = 64) ... etc ... Class MyModelAdminForm (forms.ModelForm): dynamicfield = forms.IntegerField () def __init __ (auto, * args, ** Kwargs): Super (MyModelAdminForm, itself) .__ init __ (* Args, ** kwargs) If 'example' in kwargs: # Dynamic field from away form, somehow? Diff Save (self, * ElGs, ** quoraj): Do something with #Dynamics Super Super (MimeodelAdminForm, Self). Saving (* Args, ** Quad) Category Meta: Model = MyMold Category MyModelAdmin (admin.ModelAdmin): Form = MyModelAdminForm admin.site.register (MyModel, MyModelAdmin)
Initial Object Build Is required to collect additional data during Therefore, it should only appear when a new object is being created (check 'Instance').
The problem I'm experiencing is moving the field while editing an existing object I have tried:
self-field ['dynamic field'] Widget = self Field ['Dynamicfield']. Hidden_widget ()
But it only gets rid of the text input field, not the label.
I have also tried to differentiate them (also, depending on the base code, instead of the field, super ()
calls):
Error code: / admin / db / myapp / mymodel / 6 / rendering on templateSyntaxError caught an exception: the key 'dynamicfield' was not found in the form ['dynamicfield'] in the form of selffield. Hidden = true # nothing. Self-fields: ['dynamicfield'] = none #Yonds error: TemplateSyntaxError at / admin / db / catalog / product / 6 / an exception was caught during rendering: 'NoType' object has no attribute 'label'
del self.base_fields ['dynamic field'] #yields Error: The keyboard does not have 'dynamic field' on the presentation Find
Try it out:
def __init __ (Self, * args, ** kwargs): If kwargs in 'instance': del self.base_fields ['dynamicfield'] Super (MyModelAdminForm, itself) .__ init__ (* Args, ** Kwargs)
Comments
Post a Comment