如何从管理中动态排除非模型字段?
我已向 ModelForm 添加了一个自定义字段,以供在管理中使用:
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)
该字段的目的是收集初始对象创建期间所需的附加数据。因此,它应该只在创建新对象时出现(检查“实例”)。
我遇到的问题是在编辑现有对象时使该字段消失。我已经尝试过:
self.fields['dynamicfield'].widget = self.fields['dynamicfield'].hidden_widget()
但这只会删除文本输入字段,而不是标签。
我也尝试过这些的变体(也在 super()
调用之前使用 base_fields 代替字段):
del self.fields['dynamicfield'] #yields error:
TemplateSyntaxError at /admin/db/myapp/mymodel/6/
Caught an exception 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'
在调用 super() 之前执行这些操作也尝试过,但也失败:
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
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 exception 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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
试试这个:
Try this:
如果删除不起作用(我不知道为什么)动态添加怎么办?
这里还有一个关于如何创建动态表单。 (我认为他甚至是创建 Django 的人?)
If the removal does not work (which I don't know why) what about dynamic adding?
Here is also a link on how to create dynamic forms. (I think it is even the guy who started Django?)