how to perform put/patch method for a model with an extended user(onetoone field) relationship django rest framework

Viewed 185

I have an account model generic to all user types, and when user signs up, they chose a particular role, and with post.save method the schema for that role is automatically created. I want the user to be able to update their profile(specific to their roles) after sign up. the problem is that I try using the put or patch method, it raises error that {user: {this field is required}}

models.py

class Specialist(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE, primary_key=True)
    hospital = models.ForeignKey(Hospital, on_delete=models.CASCADE, default=1, blank=True)
    description = models.TextField(blank=True)

    profile_picture = models.ImageField(upload_to='doc_image/', blank=True, null=True)

    def __str__(self):
        return f'{self.user}'

serializer.py

    class Meta:
        model = Specialist
        optional_fields = ['user', ]
        fields = ['user', 'hospital', 'description', 'profile_picture']
        extra_kwargs = {
            'user': {'read_only': False},
            'user': {'validators': []},
        }   
    def update(self, instance, validated_data):
        instance.user = validated_data.get('user', instance.user)
        instance.hospital = validated_data.get('hospital', instance.hospital)
        instance.description = validated_data.get('hospital', instance.description)
        instance.profile_picture = validated_data.get('profile_picture', instance.profile_picture)
        instance = super().update(instance, validated_data)
        return instance

views.py

class SpecialistUpdateView(APIView):
    
    def get(self, request, doctor_id):
        try:
            model = Specialist.objects.get(user_id = doctor_id)
        except Specialist.DoesNotExist:
            return Response(f'doctor with the id {doctor_id} is not found', status=status.HTTP_404_NOT_FOUND)
        serializer = SpecialistUpdateSerializer(model)
        return Response(serializer.data)
        def patch(self, request, doctor_id):
        model = Specialist.objects.get(user_id=doctor_id)
        # model = Hospital.objects.get(user_id=doctor_id)
        data = request.data
        model.user = data.get('user', model.user)
        model.hospital = data.get('hospital', model.hospital)
        model.description = data.get('description', model.description)
        model.profile_picture = data.get('profile_picture', model.profile_picture)
        model.save()
        serializer = SpecialistUpdateSerializer(model)

        return Response(serializer.data, status=status.HTTP_201_CREATED)
    def patch(self, request, doctor_id):
        model = Specialist.objects.get(user_id=doctor_id)
        # model = Hospital.objects.get(user_id=doctor_id)
        data = request.data
        model.user = data.get('user', model.user)
        model.hospital = data.get('hospital', model.hospital)
        model.description = data.get('description', model.description)
        model.profile_picture = data.get('profile_picture', model.profile_picture)
        model.save()
        serializer = SpecialistUpdateSerializer(model)

        return Response(serializer.data, status=status.HTTP_201_CREATED)


I think I'm not instantiating the user correctly. pls help. ive tried adding these codes in my serializers.py

user = serializers.PrimaryKeyRelatedField(queryset=Specialist.objects.all(), required=False,
                                                        allow_null=True, default=None)
    user = serializers.PrimaryKeyRelatedField(many=False,required=False)

but i got this error

sqlite3.IntegrityError: FOREIGN KEY constraint failed

0 Answers
Related