0

I have a model called Order, Profile and Users. In my Order model, i want to access the Profile of the buyer and seller, so i wrote a function in the Order model to get the profile of the buyer and the seller.

class Orders(models.Model):
    service = models.ForeignKey(Service, on_delete=models.SET_NULL, null=True, related_name="service_orders")
    seller = models.ForeignKey(User, on_delete=models.SET_NULL, null=True, related_name="seller")
    buyer = models.ForeignKey(User, on_delete=models.SET_NULL, null=True, related_name="buyer")
    ...

    def seller_profile(self):
        seller_profile = Profile.objects.get(user=self.seller)
        return seller_profile

    def buyer_profile(self):
        buyer_profile = Profile.objects.get(user=self.buyer)
        return buyer_profile

Now when i add the seller_profile and buyer_profile in my OrderSerializer in serializer.py, and try accessing the api endpoint in the browser, it shows the error Object of type Profile is not JSON serializable, Do i need to serialize my Profile Model or something like that?

class OrderSerializer(serializers.ModelSerializer):

    class Meta:
        model = Orders
        fields = ['id','seller', 'buyer', 'buyer_profile', 'seller_profile', ...]
       
    
    def __init__(self, *args, **kwargs):
        super(OrderSerializer, self).__init__(*args, **kwargs)
        request = self.context.get('request')
        if request and request.method=='POST':
            self.Meta.depth = 0
        else:
            self.Meta.depth = 2

I dont have any ProfileSeriallizer, do i need it?

1 Answer 1

1

models.py

class Orders(models.Model):
    service = models.ForeignKey(Service, on_delete=models.SET_NULL, null=True, related_name="service_orders")
    seller = models.ForeignKey(User, on_delete=models.SET_NULL, null=True, related_name="seller")
    buyer = models.ForeignKey(User, on_delete=models.SET_NULL, null=True, related_name="buyer")

serializers.py

class ProfileSerializer(serializers.ModelSerializer):
    class Meta:
        model = Profile
        fields = '__all__'
        depth = 1

    # Flatten
    def to_representation(self, instance):
        representation = super().to_representation(instance)
        user= representation.pop('user')
        
        for key, value in user.items():
            representation[key] = value

        return representation

class OrderSerializer(serializers.ModelSerializer):
    seller = serializers.SerializerMethodField()
    buyer = serializers.SerializerMethodField()

    class Meta:
        model = Orders
        fields = ['id','seller', 'buyer']

    def get_seller(self, obj):
        return ProfileSerializer(obj.seller.profile).data

    def get_buyer(self, obj):
       return ProfileSerializer(obj.buyer.profile).data

    
    def __init__(self, *args, **kwargs):
        super(OrderSerializer, self).__init__(*args, **kwargs)
        request = self.context.get('request')
        if request and request.method=='POST':
            self.Meta.depth = 0
        else:
            self.Meta.depth = 2
Sign up to request clarification or add additional context in comments.

2 Comments

this is not fixing the error, i have added the property method on my method
My mistake, I did not create the profile. So, yes you are going to need a serializer. Also, you do not need the model methods, instead you can access profile via relationship. I will update the answer. (I must say that, it would be probably better to use Profile directly in the model relation).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.