Field Level Permission Django

前端 未结 3 2017
一整个雨季
一整个雨季 2021-02-05 20:27

Today i came up with a requirement where i need to implement field level permission so looking for the best possible way.

class ABC(models.Model):
    fiel         


        
3条回答
  •  春和景丽
    2021-02-05 20:57

    Just in case someone else stumble about this, I had some issues with the given accepted answer. Every time the view got refreshed, it appended the fields over and over again. As well to the desired restricted view, where it shouldn't appear.

    So, according to the docs, I made it working as follows:

    Creating a custom ModelForm

    class AbcStaffForm(ModelForm):
        class Meta:
            model = Abc
            exclude = ["manager", "foo", "bar",]
    

    Overwrite get_form() in AbcModelAdmin & refered the custom ModelForm

    class AbcAdmin(admin.ModelAdmin):
        # some other code
        # ...
        def get_form(self, request, obj=None, **kwargs):
            if not request.user.is_superuser:
                kwargs['form'] = AbcStaffForm  # ModelForm
    
            return super().get_form(request, obj, **kwargs)
    

提交回复
热议问题