Answer the question
In order to leave comments, you need to log in
Django. DRF. Serializers. How to pass an additional variable to the serializer during initialization?
Good afternoon everyone!
There is a code:
class TestUpdateApiView(generics.RetrieveUpdateDestroyAPIView, TestCustomMixin):
serializer_class = TestSerializer
queryset = Test.objects.all()
lookup_field = "id"
serializer_class = TestSerializer(user=self.user)
def update(self, instance, validated_data):
#self.instance.id - ID записи на апдейт
if self.instance.id.creator == {переменная пользователя, передаваямая в Сериалайзер}
ret = super().update(instance, validated_data)
return ret
...
Answer the question
In order to leave comments, you need to log in
What?
self.instance.id.creator
instance - this is the very object from where you need to take the data.
If it’s so hot to make a comparison (it’s not clear which one, but let it be), then you can create a property in user and check it there:
class User:
@property
def is_creator(self):
# code
def update(self, instance, validated_data):
if instance.is_creator:
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question