# Django: Object of type Profile is not JSON serializable

**URL:** <https://forum.djangoproject.com/t/django-object-of-type-profile-is-not-json-serializable/18798>\
**Category:** Forms & APIs\
**Created:** [February 12, 2023, 12:09pm UTC](https://forum.djangoproject.com/t/django-object-of-type-profile-is-not-json-serializable/18798 "2023-02-12T12:09:31Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![desphixs](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/desphixs/32/6411_2.png) [@desphixs](https://forum.djangoproject.com/u/desphixs)\
**Post date:** [February 12, 2023, 12:09pm UTC](https://forum.djangoproject.com/t/django-object-of-type-profile-is-not-json-serializable/18798/1 "2023-02-12T12:09:31Z")

</div>

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.

```auto
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?

```auto
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?

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [February 12, 2023, 12:45pm UTC](https://forum.djangoproject.com/t/django-object-of-type-profile-is-not-json-serializable/18798/2 "2023-02-12T12:45:38Z")

</div>

> [@desphixs](#):
>
> Do i need to serialize my `Profile` Model

Yes.

> [@desphixs](#):
>
> I dont have any ProfileSeriallizer, do i need it?

Yes.

The DRF needs to know how to convert your Profile object into a native Python representation that can then be converted to JSON.

---

<div class="post-metadata">

**Author:** ![desphixs](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/desphixs/32/6411_2.png) [@desphixs](https://forum.djangoproject.com/u/desphixs)\
**Post date:** [February 13, 2023, 10:27am UTC](https://forum.djangoproject.com/t/django-object-of-type-profile-is-not-json-serializable/18798/3 "2023-02-13T10:27:57Z")

</div>

I have serialized my profile in serializer.py but still getting the error

```auto
class ProfileSerializer(serializers.ModelSerializer):

    class Meta:
        model = Profile
        fields = ['pid', 'user', 'full_name', 'bio', 'country', 'image', 'address', 'phone', 'website', 'pin', 'facebook', 'instagram', 'twitter', 'whatsApp', 'verified', 'wallet', 'code', 'recommended_by', 'date']

```

Is there a place i need to use the Serialized Profile in My Order Model?

---

<div class="post-metadata">

**Author:** ![leandrodesouzadev](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/leandrodesouzadev/32/8981_2.png) [@leandrodesouzadev](https://forum.djangoproject.com/u/leandrodesouzadev)\
**Post date:** [February 13, 2023, 11:02am UTC](https://forum.djangoproject.com/t/django-object-of-type-profile-is-not-json-serializable/18798/4 "2023-02-13T11:02:49Z")

</div>

You need to use this Serializer on the view that it’s getting invoked.

---

<div class="post-metadata">

**Author:** ![desphixs](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/desphixs/32/6411_2.png) [@desphixs](https://forum.djangoproject.com/u/desphixs)\
**Post date:** [February 13, 2023, 11:18am UTC](https://forum.djangoproject.com/t/django-object-of-type-profile-is-not-json-serializable/18798/5 "2023-02-13T11:18:34Z")

</div>

But i already another serializer on the view

```auto
    serializer_class = OrderSerializer

```

Can i pass in multiple serializers?

## _Edit_

I haved passed in the serilaizer along side the `OrderSerializer` like this

```auto
serializer_class = OrderSerializer, ProfileSerializer

```

Now i am getting this error

```auto
'tuple' object is not callable

```

---

<div class="post-metadata">

**Author:** ![leandrodesouzadev](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/leandrodesouzadev/32/8981_2.png) [@leandrodesouzadev](https://forum.djangoproject.com/u/leandrodesouzadev)\
**Post date:** [February 13, 2023, 11:21am UTC](https://forum.djangoproject.com/t/django-object-of-type-profile-is-not-json-serializable/18798/6 "2023-02-13T11:21:47Z")

</div>

> [@desphixs](#):
>
> Can i pass in multiple serializers?

No.  
I suggest that you review this section of the [drf docs](https://www.django-rest-framework.org/api-guide/relations/#nested-relationships).  
You need to tell to your `OrderSerializer` to use the `ProfileSerializer` for both of these keys: `buyer_profile`, `seller_profile`.  
The very first example on the docs shows how to do this.

---

<div class="post-metadata">

**Author:** ![desphixs](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/desphixs/32/6411_2.png) [@desphixs](https://forum.djangoproject.com/u/desphixs)\
**Post date:** [February 13, 2023, 4:30pm UTC](https://forum.djangoproject.com/t/django-object-of-type-profile-is-not-json-serializable/18798/7 "2023-02-13T16:30:04Z")

</div>

That fixed it for me, thank you very much for your help.
