Django 管理中“__init__”中的动态表单字段

发布于 2024-11-25 08:57:00 字数 691 浏览 3 评论 0原文

我希望能够在运行时向 django 管理表单添加字段。我的模型和表单:

#admin.py
class SitesForm(forms.ModelForm):

    def __init__(self, *args, **kwargs):
        super(SitesForm, self).__init__(*args, **kwargs)

        self.fields['mynewfield'] = forms.CharField()

class SitesAdmin(admin.ModelAdmin):
    form = SitesForm

admin.site.register(Sites,SitesAdmin)

#model.py
class Sites(models.Model):
    url = models.URLField(u'URL')
    is_active = models.BooleanField(default=True, blank=True)
    is_new = models.BooleanField(default=False, blank=True)
    group = models.ForeignKey('SitesGroup')
    config = models.TextField(blank=True)

字段 mynewfield 未显示在表单中。为什么?

I want to be able to add fields to django admin form at runtime. My model and form:

#admin.py
class SitesForm(forms.ModelForm):

    def __init__(self, *args, **kwargs):
        super(SitesForm, self).__init__(*args, **kwargs)

        self.fields['mynewfield'] = forms.CharField()

class SitesAdmin(admin.ModelAdmin):
    form = SitesForm

admin.site.register(Sites,SitesAdmin)

#model.py
class Sites(models.Model):
    url = models.URLField(u'URL')
    is_active = models.BooleanField(default=True, blank=True)
    is_new = models.BooleanField(default=False, blank=True)
    group = models.ForeignKey('SitesGroup')
    config = models.TextField(blank=True)

Field mynewfield isn't displayed in form. Why?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

鲜肉鲜肉永远不皱 2024-12-02 08:57:00

您不应该以这种方式向表单添加新字段,您可以像添加任何其他字段一样执行此操作,并且表单将包含模型的原始字段和新字段:

class SitesForm(forms.ModelForm):
  mynewfield = forms.CharField(max_length=255, blank=True)

  class Meta:
    model = Sites

class SitesAdmin(admin.ModelAdmin):
  form = SitesForm

admin.site.register(Sites, SitesAdmin)

编辑 : 抱歉,应该读一下你写的更好一点。如果您想要一个这样的动态字段,那么您需要执行以下操作,它将完全按照您的要求进行操作:

class SitesForm(forms.ModelForm):
  class Meta:
    model = Sites

  def __init__(self, *args, **kwargs):
    self.base_fields['mynewfield'] = forms.CharField(max_length=255, blank=True)
    super(SitesForm, self).__init__(*args, **kwargs)

class SitesAdmin(admin.ModelAdmin):
  form = SitesForm

admin.site.register(Sites, SitesAdmin)

它是由保存表单将使用的字段的元类组成的 base_fields 。

You shouldn't be adding a new field to your form in that way, you can just do it as you would any other field and the form will contain both the Model's original fields and your new fields:

class SitesForm(forms.ModelForm):
  mynewfield = forms.CharField(max_length=255, blank=True)

  class Meta:
    model = Sites

class SitesAdmin(admin.ModelAdmin):
  form = SitesForm

admin.site.register(Sites, SitesAdmin)

Edit: Sorry, should have read what you had written a little better. If you want a dynamic field like that, then you need to do the following and it will do exactly what you want:

class SitesForm(forms.ModelForm):
  class Meta:
    model = Sites

  def __init__(self, *args, **kwargs):
    self.base_fields['mynewfield'] = forms.CharField(max_length=255, blank=True)
    super(SitesForm, self).__init__(*args, **kwargs)

class SitesAdmin(admin.ModelAdmin):
  form = SitesForm

admin.site.register(Sites, SitesAdmin)

It's the base_fields that gets composed by the metaclass that holds the fields that the form will use.

醉城メ夜风 2024-12-02 08:57:00

解决方案:

class AdminForm(forms.ModelForm):
    def __init__(self, *args, **kwargs):
        super(AdminForm, self).__init__(*args, **kwargs)
        self.fields.insert(1, 'myfield', forms.CharField())

class MyAdmin(admin.ModelAdmin):
    form = AdminForm

    def get_fieldsets(self, request, obj=None):
        return (
            (None, {
                'fields': (..., 'myfield',),
            }),
        )

Solution:

class AdminForm(forms.ModelForm):
    def __init__(self, *args, **kwargs):
        super(AdminForm, self).__init__(*args, **kwargs)
        self.fields.insert(1, 'myfield', forms.CharField())

class MyAdmin(admin.ModelAdmin):
    form = AdminForm

    def get_fieldsets(self, request, obj=None):
        return (
            (None, {
                'fields': (..., 'myfield',),
            }),
        )
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文