Answer the question
In order to leave comments, you need to log in
How to add ManyToMany using perform_update?
Model Part :
class Entry(models.Model):
user = models.ForeignKey(User, related_name='entries')
drivers = models.ManyToManyField(Driver, related_name='entries', blank=True, null=True)
...
class EntrySerializer(serializers.ModelSerializer):
class Meta:
model = Entry
fields = '__all__'
extra_kwargs = {'user': {'read_only': True}, 'driver': {'read_only': True}}
class SetAsDriver(generics.UpdateAPIView):
permission_classes = (IsAuthenticated, IsDriver)
queryset = Entry.objects.all()
serializer_class = EntrySerializer
class SetAsDriver(generics.UpdateAPIView):
permission_classes = (IsAuthenticated, IsDriver)
queryset = Entry.objects.all()
serializer_class = EntrySerializer
def perform_create(self,serializer):
serializer.data.drivers.add(self.request.user.driver)
serializer.save()
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