I've added a custom field to a ModelForm for use in the admin:
class MyModel(models.Model):
name = models.CharField(max_length=64)
...etc...
class MyModelAdminForm(forms.ModelForm):
dynamicfield = forms.IntegerField()
def __init__(self, *args, **kwargs):
super(MyModelAdminForm, self).__init__(*args, **kwargs)
if 'instance' in kwargs:
#remove dynamicfield from the form, somehow?
def save(self, *args, **kwargs):
#do something with dynamicfield
super(MyModelAdminForm, self).save(*args, **kwargs)
class Meta:
model = MyModel
class MyModelAdmin(admin.ModelAdmin):
form = MyModelAdminForm
admin.site.register(MyModel, MyModelAdmin)
The point of this field is to collect additional data needed during initial object creation. Therefore it should only appear when a new object is being created (the check for 'instance').
The problem I'm having is making the field go away when editing an existing object. I've tried:
self.fields['dynamicfield'].widget = self.fields['dynamicfield'].hidden_widget()
but that only gets rid of the text input field, not the label.
I've also tried variations on these (also using base_fields in place of fields, before the super()
call):
del self.fields['dynamicfield'] #yields error:
TemplateSyntaxError at /admin/db/myapp/mymodel/6/
Caught an exc开发者_StackOverflow社区eption while rendering: Key 'dynamicfield' not found in Form
self.fields['dynamicfield'].hidden = True #Does nothing.
self.fields['dynamicfield'] = None #yields error:
TemplateSyntaxError at /admin/db/catalog/product/6/
Caught an exception while rendering: 'NoneType' object has no attribute 'label'
Doing these before the call to super() also tried, but also fail:
del self.base_fields['dynamicfield'] #yields error:
TemplateSyntaxError at /admin/db/myapp/mymodel/6/
Caught an exception while rendering: Key 'dynamicfield' not found in Form
Try this:
def __init__(self, *args, **kwargs):
if 'instance' in kwargs:
del self.base_fields['dynamicfield']
super(MyModelAdminForm, self).__init__(*args, **kwargs)
If the removal does not work (which I don't know why) what about dynamic adding?
def __init__(self, *args, **kwargs):
super(MyModelAdminForm, self).__init__(*args, **kwargs)
if 'instance' not in kwargs:
self.fields['dynamicfield'] = forms.IntegerField()
Here is also a link on how to create dynamic forms. (I think it is even the guy who started Django?)
精彩评论