I have a model called worker
class Worker(models.Model):
name = models.CharField(max_length=300)
code = models.CharField(max_length=100)
user = models.OneToOneField(User, null=True, blank=True)
def __str__(self):
return self.name
Which has a 1 to 1 relationship with the user model of django This is my serializer:
class WorkerSerializer(serializers.ModelSerializer):
class Meta:
model = Worker
fields = '__all__'
and This my viewset:
@transaction.atomic
def update(self, request, *args, **kwargs):
with transaction.atomic():
try:
instance = self.get_object()
instance.id = kwargs.get('pk')
serializer = WorkerSerializer(instance=instance, data=request.data)
if serializer.is_valid(raise_exception=True):
self.perform_update(serializer)
return Response({"status": True, "results": "Datos actualizados correctamente"},
status=status.HTTP_201_CREATED)
except ValidationError as err:
return Response({"status": False, "error_description": err.detail}, status=status.HTTP_400_BAD_REQUEST)
The problem I have is that when wanting to update the user field, it asks for the id of the user but I want to update it by putting in the client its username how can I do this? and how do I verify if that worker already has an associated user ?