Answer the question
In order to leave comments, you need to log in
How do I properly create objects in django using drf?
Model:
class ProjectInvite(models.Model):
project = models.ForeignKey(Project, on_delete=models.CASCADE, related_name='project_invites')
from_user = models.ForeignKey(get_user_model(), on_delete=models.CASCADE, related_name='sended_project_invites')
to_user = models.ForeignKey(get_user_model(), on_delete=models.CASCADE, related_name='came_project_invites')
class ProjectInviteSerializer(serializers.ModelSerializer):
from_user = UserSerializer(many=False, read_only=True)
to_user = UserSerializer(many=False)
project = ProjectSerializer(many=False)
class Meta:
model = models.ProjectInvite
fields = ('id', 'from_user', 'to_user', 'project')
class ProjectInviteViewSet(viewsets.ModelViewSet):
serializer_class = serializers.ProjectInviteSerializer
queryset = models.ProjectInvite.objects
def perform_create(self, serializer):
serializer.save(from_user=self.request.user)
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question