Hello guys and girls,
Just having a little issue with Django Rest Framework : let's say I have a Book class :
models.py
class Books(models.Model):
title = models.Charfield(max_length = 50, blank=False)
serializers.py
class BooksSerializer (serializers.ModelSerializer)
class Meta:
model = Books
fields = ['id', 'title']
And now I want to allow the create method only until I have 30 books in database.
This is the kind of code I have until now but it isn't working so far, how would you recommend me to proceed ? I can override the create method from the ModelViewSet class right ?
views.py
class BooksViewset(viewsets.ModelViewSet):
queryset = Books.objects.all()
serializer_class = BooksSerializer
def create(self, serializer):
number_of_books = queryset.count()
if number of book < 30:
serializer.save()
else:
response = {'message': 'You can't create more than 30 values'}
return Response(response, status=status.HTTP_400_BAD_REQUEST)
Thank you in advance for your help