如何将自定义字段添加到 InlineFormsets?

发布于 2024-07-13 04:46:52 字数 1064 浏览 9 评论 0原文

我尝试使用以下代码将自定义字段添加到 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

各自安好 2024-07-20 04:46:52

我用另一种方式做到了:

forms.py:

from django import forms
class ItemAddForm(forms.ModelForm):
    my_new_field = forms.IntegerField(initial=1, label='quantity')
    class Meta:
        model = Item

admin.py:

from django.contrib import admin
from forms import *
class ItemAddInline(admin.TabularInline):
    form = ItemAddForm
    fields = (..., 'my_new_field')

到目前为止,这有效,我只需要以某种方式重写 save 方法来处理这个新字段。 请参阅:http://docs.djangoproject.com/en/ dev/ref/contrib/admin/#form 。 它说默认情况下内联使用 BaseModelForm,它被发送到 formset_factory。 它对我不起作用,尝试对 BaseModelForm 进行子类化但出现错误(没有属性“_meta”)。 所以我改用 ModelForm。

I did it another way:

forms.py:

from django import forms
class ItemAddForm(forms.ModelForm):
    my_new_field = forms.IntegerField(initial=1, label='quantity')
    class Meta:
        model = Item

admin.py:

from django.contrib import admin
from forms import *
class ItemAddInline(admin.TabularInline):
    form = ItemAddForm
    fields = (..., 'my_new_field')

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.

听不够的曲调 2024-07-20 04:46:52

您可以通过另一种方式(动态表单)来做到这一点:

admin.py

class ProgressInline(admin.TabularInline):
    model = models.Progress
    extra = 8

    def get_formset(self, request, obj=None, **kwargs):
        extra_fields = {'my_field': forms.CharField()}
        kwargs['form'] = type('ProgressForm', (forms.ModelForm,), extra_fields)
        return super(ProgressInline, self).get_formset(request, obj, **kwargs)

You can do it by another way (Dynamic forms):

admin.py

class ProgressInline(admin.TabularInline):
    model = models.Progress
    extra = 8

    def get_formset(self, request, obj=None, **kwargs):
        extra_fields = {'my_field': forms.CharField()}
        kwargs['form'] = type('ProgressForm', (forms.ModelForm,), extra_fields)
        return super(ProgressInline, self).get_formset(request, obj, **kwargs)
最笨的告白 2024-07-20 04:46:52
model = models.Progress

在管理中,只有在此 Progress 模型中定义的字段。 您没有覆盖它的字段/字段集选项。

如果您想添加新字段,有两个选项:

  • 在模型定义中,添加这些新的附加字段(使它们成为可选!)
  • 在管理模型 (admin.TabularInline) 中,添加类似的内容:

    fields = ('newfield1', 'newfield2', 'newfield3')

看看 字段< /a>, 字段集

model = models.Progress

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 model definition, add those new additional fields (make them optional!)
  • In the admin model (admin.TabularInline), add something something like:

    fields = ('newfield1', 'newfield2', 'newfield3')

Take a look at fields, fieldsets.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文