Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django REST Framework different depth for POST/PUT?

I am using Django REST Framework to create an API for my web app. I have a class 'Comment', that has depth=2 set in the Meta class. This works great when GETing the Comments. When I try to send a POST or PUT request though (i.e. create a new Comment) I am told I need to include objects instead of ForeignKey IDs.

Here's my Serializer class:

class CommentSerializer(serializers.ModelSerializer):
    class Meta:
        model = Comment
        depth = 2 

The model:

class Comment(models.Model):
    user = models.ForeignKey(User, null=True, blank=True,
        related_name='comments')
    budget = models.ForeignKey(Budget, related_name='comments')
    published = models.BooleanField(default=False)
    body = models.TextField()
    created = models.DateTimeField(auto_now_add=True)

The view code:

class Comments(generics.ListCreateAPIView):

    model = Comment
    serializer_class = CommentSerializer

    def pre_save(self, obj):
        obj.user = self.request.user

And the error that is displayed in the output (JSON) is:

{"user": ["This field is required."], "budget": [{"non_field_errors": ["Invalid data"]}]}

When this raw data is sent:

{"budget": 2, "published": true, "body": "Another comment"}
like image 426
Jeremy Blalock Avatar asked Apr 08 '13 15:04

Jeremy Blalock


4 Answers

I had the same problem so I Solved making custom generic methods.This is better implementation of above answers

class CustomListCreateAPIView(mixins.ListModelMixin,
                              mixins.CreateModelMixin,
                              generics.GenericAPIView):
    """
    Concrete view for listing a queryset or creating a model instance.
    """
    def get_serializer_class(self):
        method = self.request.method
        if method == 'PUT' or method == 'POST':
            return self.writeSerializers
        else:
            return self.readSerializers

    def get(self, request, *args, **kwargs):
        return self.list(request, *args, **kwargs)

    def post(self, request, *args, **kwargs):
        return self.create(request, *args, **kwargs)

Similarily RUD,

class CustomRetrieveUpdateDestroyAPIView(mixins.RetrieveModelMixin,
                                         mixins.UpdateModelMixin,
                                         mixins.DestroyModelMixin,
                                         generics.GenericAPIView):
    """
    Concrete view for retrieving, updating or deleting a model instance.
    """
    def get_serializer_class(self):
        method = self.request.method
        if method == 'PUT' or method == 'POST':
            return self.writeSerializers
        else:
            return self.readSerializers

    def get(self, request, *args, **kwargs):
        return self.retrieve(request, *args, **kwargs)

    def put(self, request, *args, **kwargs):
        return self.update(request, *args, **kwargs)

    def patch(self, request, *args, **kwargs):
        return self.partial_update(request, *args, **kwargs)

    def delete(self, request, *args, **kwargs):
        return self.destroy(request, *args, **kwargs)  # enter code here

Now I just give writeSerializers and readSerializers values in Views.py

Also to create Read-write Serializers there is an easy way.

class employeeWriteSerializer(serializers.ModelSerializer):
    class Meta:
        model = employee
        fields = ('username','email',..)
        
class employeeReadSerializer(serializers.ModelSerializer):
     class Meta(employeeWriteSerializer.Meta):
         depth = 1

It saves time and repetitive work you can also add authentication classes in custom generic Api(Retitve work). Thanks.

like image 190
Piyush Bali Avatar answered Oct 02 '22 13:10

Piyush Bali


You can set different serializers by overriding the get_serializer_class() function, like so:

def get_serializer_class(self): method = self.request.method if method == 'PUT' or method == 'POST': return YourWriteSerializer else: return YourReadSerializer

I thought to add this one, since i came here from Googling after a while.

like image 24
Kermet Avatar answered Oct 19 '22 09:10

Kermet


I know this is a little bit late but I ended up using 2 serializers like so:

class CommentReadSerializer(serializers.ModelSerializer):
    class Meta:
        model = Comment
        depth = 2

class CommentWriteSerializer(serializers.ModelSerializer):
    class Meta:
        model = Comment

Then used like this:

class Comments(generics.ListCreateAPIView):

    model = Comment
    serializer_class = CommentReadSerializer

    def create(self, request, *args, **kwargs):
        serializer = CommentWriteSerializer(data=request.DATA, files=request.FILES)
        if serializer.is_valid():
            self.pre_save(serializer.object)
            self.object = serializer.save(force_insert=True)
            self.post_save(self.object, created=True)
            headers = self.get_success_headers(serializer.data)
            serializer = CommentReadSerializer(serializer.object)
            return Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers)

        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
like image 8
Robin Elvin Avatar answered Oct 19 '22 08:10

Robin Elvin


I believe the proper way to define a serializer field that refers to a foreign key relationship is through something like serializers.PrimaryKeyRelatedField. I don't believe that model serializers automatically use this field class without defining it explicitly in the serializer class.

http://www.django-rest-framework.org/api-guide/relations/#primarykeyrelatedfield

I would imagine that a PrimaryKeyRelatedField serializer would correctly handle JSON data submissions like the one you used in your example.

like image 6
David Sanders Avatar answered Oct 19 '22 08:10

David Sanders