one to one field update serialization issue

To perform the update, you have to override the update method in the serializer.

def update(self, instance, validated_data):
  instance.profile.profile_id 
 = validated_data["profile"].get('profile_id',instance.profile.profile_id)
  instance.amt = validated_data.get('amt', instance.amt) 
  return instance

in your viewset

def update(self,request,pk=None):
   profile_id = request.data.get("profile_id")
   profile = Profile.objects.get(profile_id = profile_id)
   ser = self.get_serializer_class()(instance=profile.cash_balance , data = request.data)
   ser.is_valid(raise_exception=True)
   ser.save()

in your CashBalanceModel, pass a related_name

profile = models.OneToOneField(Profile, on_delete=models.CASCADE,db_column="profile_id" , related_name="cash_balance")

check docs: https://www.django-rest-framework.org/api-guide/serializers/

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top