Dynamic fields in Django Admin

后端 未结 9 1468
失恋的感觉
失恋的感觉 2020-12-05 05:18

I want to have additional fields regarding value of one field. Therefor I build a custom admin form to add some new fields.

Related to the blogpost of jacobian 1 thi

9条回答
  •  遥遥无期
    2020-12-05 05:35

    Stephan's answer is elegant, but when I used in in dj1.6 it required the field to be a tuple. The complete solution looked like this:

    class ProductForm(ModelForm):
        foo = CharField(label='foo')
    
    
    class ProductAdmin(admin.ModelAdmin):
        form = ProductForm
        def get_fieldsets(self, request, obj=None):
            fieldsets = super(ProductAdmin, self).get_fieldsets(request, obj)
            fieldsets[0][1]['fields'] += ('foo', ) 
            return fieldsets
    

提交回复
热议问题