1👍
✅
The issue is in the to_representation
method for the serializer.
def to_representation(self, instance):
representation = super().to_representation(instance)
representation["scheduled_datetime"] = instance.LeadFacilityAssociation.first().scheduled_datetime
representation["assigned_facilities"] = instance.LeadFacilityAssociation.first().assigned_facilities
print(instance.LeadFacilityAssociation.first().assigned_facilities)
return representation
The issue is that in this part representation["assigned_facilities"] = instance.LeadFacilityAssociation.first().assigned_facilities
an instance of the assigned facility is returned and as the error message says instances are not JSON serializable.
What you can do is assign representation["assigned_facilities"]
to the assigned facilities name
or id
like this:
def to_representation(self, instance):
representation = super().to_representation(instance)
representation["scheduled_datetime"] = instance.LeadFacilityAssociation.first().scheduled_datetime
# You can use the name or whatever field fits similar to what you have above
representation["assigned_facilities"] = instance.LeadFacilityAssociation.first().assigned_facilities.id
print(instance.LeadFacilityAssociation.first().assigned_facilities)
return representation
Source:stackexchange.com