KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
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 GET ing 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"}
Tags (comma-separated)
Save Edits
Cancel