Package: djangorestframework-fine-permissions
New permissions possibilities for rest-frameworkCompatibilityWorks with :Python 3.8, 3.9, 3.10Django 3.2Django Rest Framework >= 3.13InstallationInstall the package from pypi :pipinstalldjangorestframework-fine-permissionsConfigure yoursettings.pymodule :INSTALLED_APPS=(...'rest_framework_fine_permissions',)REST_FRAMEWORK={'DEFAULT_FILTER_BACKENDS':(# Enable the filter permission backend for all GenericAPIView'rest_framework_fine_permissions.filters.FilterPermissionBackend',),'DEFAULT_PERMISSION_CLASSES':(# Enable the django model permissions (view,create,delete,modify)'rest_framework_fine_permissions.permissions.FullDjangoModelPermissions',# OPTIONAL if you use FilterPermissionBackend and GenericAPIView. Check filter permissions for objects.'rest_framework_fine_permissions.permissions.FilterPermission',)}Sync the django’s database :pythonmanage.pysyncdbEdit yoururls.pymodule :fromdjango.conf.urlsimporturlfromdjango.contribimportadminfromrest_framework_fine_permissions.urlsimporturlpatternsasdrffp_urlsurlpatterns=[url(r'^admin/',admin.site.urls),]urlpatterns+=drffp_urlsUsageGo to the django admin pageAdd field’s permissions to a user with the “User fields permissions” linkAdd filter’s permissions to a user with the “User filters permissions” linkExamplemodels.py:fromdjango.dbimportmodelsfromdjango.db.modelsimportSumclassPollsChoice(models.Model):id=models.IntegerField(primary_key=True)choice_text=models.CharField(max_length=200)votes=models.IntegerField()question=models.ForeignKey('PollsQuestion')classMeta:permissions=(('view_pollschoice','Can view pollschoice'),)classPollsQuestion(models.Model):id=models.IntegerField(primary_key=True)question_text=models.CharField(max_length=200)pub_date=models.DateTimeField()classMeta:permissions=(('view_pollsquestion','Can view pollsquestion'),)@propertydefsum_votes(self):returnself.pollschoice_set.aggregate(total=Sum('votes'))['total']@propertydefchoices(self):returnself.pollschoice_set.all()serializers.py:importdatetimefromdjango.utilsimporttimezonefromrest_frameworkimportserializersfromrest_framework_fine_permissions.fieldsimportModelPermissionsFieldfromrest_framework_fine_permissions.serializersimportModelPermissionsSerializerfrom.importmodelsclassPollsChoiceSerializer(ModelPermissionsSerializer):classMeta:model=models.PollsChoiceclassPollsQuestionSerializer(ModelPermissionsSerializer):was_published_recently=serializers.SerializerMethodField()votes=serializers.IntegerField(source='sum_votes')choices=ModelPermissionsField(PollsChoiceSerializer)classMeta:model=models.PollsQuestiondefget_was_published_recently(self,obj):returnobj.pub_date>=timezone.now()-datetime.timedelta(days=1)views.py:from.importmodelsfrom.importserializersfromrest_frameworkimportgenericsclassPollsChoiceDetail(generics.RetrieveUpdateDestroyAPIView):queryset=models.PollsChoice.objects.all()serializer_class=serializers.PollsChoiceSerializerurls.py:fromdjango.conf.urlsimportpatterns,urlfromrest_framework.urlpatternsimportformat_suffix_patternsfrom.importviewsurlpatterns=[,url(r'^pollsquestion/(?P<pk>\w+)$',views.PollsQuestionDetail.as_view(),name='pollsquestion-all-detail'),]urlpatterns=format_suffix_patterns(urlpatterns,suffix_required=True)Create a user without the staff and superuser status, and add him permissions :Then add user field permissions :You can finally call your webservice :$curl-XGET-H"Authorization: Token TOKEN"-H"Accept: application/json; indent=4"http://127.0.0.1/webservice/pollsquestion/1.json{"choices":[{"choice_text":"Yes","id":1,"votes":5},{"choice_text":"No","id":2,"votes":2}],"id":1,"pub_date":"2017-01-08T09:00:00","question_text":"Is this a question ?","votes":7,"was_published_recently":false}Import/ExportTo export field’s permissions, you can use the following command :python manage.py fine_permissions_dump myuser > /tmp/myuserfieldsperms.jsonTo import field’s permissions, you can use the following command :python manage.py fine_permissions_load -u anotheruser /tmp/myuserfieldsperms.json