Dynamical Form fields in __init__in Django admin

☆樱花仙子☆ 提交于 2020-04-13 18:12:12

问题


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' isnt displaying in form. Why?


回答1:


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.




回答2:


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',),
            }),
        )


来源:https://stackoverflow.com/questions/6788189/dynamical-form-fields-in-init-in-django-admin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!