如何将自定义字段添加到 InlineFormsets?
我尝试使用以下代码将自定义字段添加到 InlineFormset,但这些字段不会显示在 Django 管理中。 InlineFormset 是否过于锁定而不允许这样做? 我的打印“ding”测试按预期触发,我可以打印出 form.fields 并在那里看到它们,但实际的字段永远不会在管理中呈现。
admin.py
from django.contrib import admin
import models
from django.forms.models import BaseInlineFormSet
from django import forms
from forms import ProgressForm
from django.template.defaultfilters import slugify
class ProgressInlineFormset(BaseInlineFormSet):
def add_fields(self, form, index):
print "ding"
super(ProgressInlineFormset, self).add_fields(form, index)
for criterion in models.Criterion.objects.all():
form.fields[slugify(criterion.name)] = forms.IntegerField(label=criterion.name)
class ProgressInline(admin.TabularInline):
model = models.Progress
extra = 8
formset = ProgressInlineFormset
class ReportAdmin(admin.ModelAdmin):
list_display = ("name", "pdf_column",)
search_fields = ["name",]
inlines = (ProgressInline,)
admin.site.register(models.Report, ReportAdmin)
I'm trying to add custom fields to an InlineFormset using the following code, but the fields won't show up in the Django Admin. Is the InlineFormset too locked down to allow this? My print "ding" test fires as expected, I can print out the form.fields and see them all there, but the actual fields are never rendered in the admin.
admin.py
from django.contrib import admin
import models
from django.forms.models import BaseInlineFormSet
from django import forms
from forms import ProgressForm
from django.template.defaultfilters import slugify
class ProgressInlineFormset(BaseInlineFormSet):
def add_fields(self, form, index):
print "ding"
super(ProgressInlineFormset, self).add_fields(form, index)
for criterion in models.Criterion.objects.all():
form.fields[slugify(criterion.name)] = forms.IntegerField(label=criterion.name)
class ProgressInline(admin.TabularInline):
model = models.Progress
extra = 8
formset = ProgressInlineFormset
class ReportAdmin(admin.ModelAdmin):
list_display = ("name", "pdf_column",)
search_fields = ["name",]
inlines = (ProgressInline,)
admin.site.register(models.Report, ReportAdmin)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我用另一种方式做到了:
forms.py:
admin.py:
到目前为止,这有效,我只需要以某种方式重写 save 方法来处理这个新字段。 请参阅:http://docs.djangoproject.com/en/ dev/ref/contrib/admin/#form 。 它说默认情况下内联使用 BaseModelForm,它被发送到 formset_factory。 它对我不起作用,尝试对 BaseModelForm 进行子类化但出现错误(没有属性“_meta”)。 所以我改用 ModelForm。
I did it another way:
forms.py:
admin.py:
This works so far, I only need to override somehow the save method to handle this new field. See this: http://docs.djangoproject.com/en/dev/ref/contrib/admin/#form . It says that by default Inlines use BaseModelForm, which is send to formset_factory. It doesn't work for me, tried to subclass BaseModelForm with errors (no attribute '_meta'). So I use ModelForm instead.
您可以通过另一种方式(动态表单)来做到这一点:
admin.py
You can do it by another way (Dynamic forms):
admin.py
在管理中,只有在此 Progress 模型中定义的字段。 您没有覆盖它的字段/字段集选项。
如果您想添加新字段,有两个选项:
在管理模型 (admin.TabularInline) 中,添加类似的内容:
fields = ('newfield1', 'newfield2', 'newfield3')
看看 字段< /a>, 字段集。
In the admin there will be only the fields defined in this Progress model. You have no fields/fieldsets option overwriting it.
If you want to add the new ones, there are two options:
In the admin model (admin.TabularInline), add something something like:
fields = ('newfield1', 'newfield2', 'newfield3')
Take a look at fields, fieldsets.