#Error: Object of type User is not JSON serializable

3 messages · Page 1 of 1 (latest)

marsh tundra
#

I want to create serializers for my models : 1. User Model of django.auth (for which I have successfully created); and this user model is foreign key to another model as follows:

models.py

class cb_data(models.Model):
    fk = models.ForeignKey(User , null =True,  on_delete=models.CASCADE)
    #contacts
    prefix = models.CharField(max_length = 4)
    phone = models.CharField(max_length = 10)
 
    #and many more fields...

My serializer.py for the models are as follows:

class CB_data_Serializer(serializers.ModelSerializer):

    # fk = cb_user_Serializer(many = True)
    class Meta:
        model = cb_data
        fields = ['id',
                 'landmark','city','state','country','pincode', 
                  'prefix', 'phone','visual_impaired','wheelchair_user',
                  'hearing_impaired','speech_impaired','roomtype1','roomtype2',
                  'roomtype3', 'pricetype1','pricetype2','pricetype3','numOftype1',
                  'numOftype2','numOftype3','facilityoftype1','facilityoftype2',
                  'facilityoftype3'
                ]
        

class cb_user_Serializer(serializers.ModelSerializer):
    data = CB_data_Serializer(many = True)

    class Meta:
        model = cb
        fields = [
            'id','username', 'first_name', 'email','data'
        ]

I am getting this error as stated.. Please help me with the same

past zenith
#

what is cb?

And maybe show how you serialize the queryset or the instance

marsh tundra