Dynamic forms in django-admin

前端 未结 1 744
走了就别回头了
走了就别回头了 2021-02-04 15:38

I want to make admin add-form dynamic. I want to add few formfields depending on setting in related object.

I have something like this:

class Classifieds         


        
1条回答
  •  既然无缘
    2021-02-04 16:32

    I've managed to do it using type().

    class ClassifiedAdmin(admin.ModelAdmin):
    
     def get_form(self, request, obj=None, **kwargs):
    
        adminform = ClassifiedsAdminForm()
        fields = adminform.getNewFields()
    
        form = type('ClassifiedsAdminForm', (forms.ModelForm,), fields)
    
        return form
    

    Hope it will help someone.

    0 讨论(0)
提交回复
热议问题