Reputation: 61
I am trying to list all the doubtclasses model using doubtclass view but there is some recursion error in the post request, which i am not able to understand , i search across for same error and i have tried if i made a similar mistake to the other developers that have asked the same question but as far as i searched mine one is different
My doubtclass view
class DoubtClass(LoginRequiredMixin, mixins.ListModelMixin, mixins.CreateModelMixin, generics.GenericAPIView):
serializer_class = serializers.DoubtClass_serializer
queryset = models.DoubtClasses.objects.filter(is_draft=False)
def get(self, request):
print("error in doubtclass get")
return self.list(request)
def post(self, request):
if request.user.is_superuser:
return self.post(request)
else:
return Response(status=status.HTTP_403_FORBIDDEN)
my doubtclass model
class DoubtClasses(models.Model):
doubtClass_details = models.TextField()
class_time = models.DateTimeField()
end_time = models.DateTimeField()
doubtsAddressed = models.IntegerField(default=0)
no_of_students_registered = models.IntegerField(default=0)
no_of_students_attended = models.IntegerField(default=0)
mentor_id = models.ForeignKey(Mentor, on_delete=models.CASCADE, null=True)
is_draft = models.BooleanField(default=True)
class Meta:
verbose_name_plural = 'DoubtClasses'
def __str__(self):
return self.doubtClass_details
I am new to django
Upvotes: 2
Views: 1908
Reputation: 21812
From your code it seems you want to limit post
requests to superusers. The problem with your implementation is that you are just calling post
again recursively. Seeing as you inherit from CreateModelMixin
you likely want to call create
instead:
class DoubtClass(LoginRequiredMixin, mixins.ListModelMixin, mixins.CreateModelMixin, generics.GenericAPIView):
serializer_class = serializers.DoubtClass_serializer
queryset = models.DoubtClasses.objects.filter(is_draft=False)
def get(self, request, *args, **kwargs):
print("error in doubtclass get")
return self.list(request)
def post(self, request, *args, **kwargs):
if request.user.is_superuser:
return self.create(request, *args, **kwargs) # call `create` instead
else:
return Response(status=status.HTTP_403_FORBIDDEN)
But this can be improved. Firstly instead of inheriting from ListModelMixin
, CreateModelMixin
and GenericAPIView
you can simply reduce that to inheriting from generics.ListCreateAPIView
. Next instead of using LoginRequiredMixin
it is better to use the IsAuthenticated
permission. Also for your limitation of POST requests being limited to superusers that can also be added to a custom permission:
from rest_framework.permissions import BasePermission, IsAuthenticated, SAFE_METHODS
class IsSuperuserOrReadOnly(BasePermission):
def has_permission(self, request, view):
return bool(
request.method in SAFE_METHODS or
request.user and
request.user.is_superuser
)
class DoubtClass(generics.ListCreateAPIView):
serializer_class = serializers.DoubtClass_serializer
queryset = models.DoubtClasses.objects.filter(is_draft=False)
permission_classes = [IsAuthenticated, IsSuperuserOrReadOnly]
Upvotes: 1