Remove fields from ModelForm

前端 未结 5 979
忘掉有多难
忘掉有多难 2020-12-09 06:16

i have a simple ModelForm:

class MyForm(ModelForm):

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


        
5条回答
  •  不思量自难忘°
    2020-12-09 06:30

    I had the same problem. This is how I made it work in the new Django (trunk):

    class MyModelAdmin(admin.ModelAdmin):
        # Your stuff here..
    
        def get_form(self, request, obj=None, **kwargs):
            if request.user.is_staff: # condition
                self.exclude = ('field',)
            return super(PublishAdmin, self).get_form(request, obj=obj, **kwargs)
    

    By overriding the get_form method and putting the logic here you can select which Form you want to have displayed. Above I have displayed the standard form when a condition was met.

提交回复
热议问题