I am working on a small project but i cant get the serializers to work
I have tried these and i am not sure what i am doing wrong. My model is a many to many through
#serializers.py
class CompanyVisitedSerializer(serializers.HyperlinkedModelSerializer):
company_id = serializers.ReadOnlyField(source="company.id")
company_address = serializers.ReadOnlyField(source="company.business_address")
company_name = serializers.ReadOnlyField(source="company.business_name")
company_address = serializers.ReadOnlyField(source="company.business_address")
checked_in = serializers.ReadOnlyField(source="user.checked_in")
checked_out = serializers.ReadOnlyField(source="user.checked_out")
class Meta:
model = CompanyUser
fields = (
"checked_in",
"checked_out",
"company_name",
"company_address",
"company_id",
)
class VisitorSerializer(serializers.ModelSerializer):
# visited = CompanySerializer(many=True)
visited = CompanyVisitedSerializer(
many=True,
)
```
but i get this back empty {}:
{
"address": "192 millie road",
"city": "singapore",
"phone": "2066980",
...
"visited": [
{}, //i want to populate this with { company: name_of_company, checkin, checkout}
{}
]
}
I have read through these:
The model to which visited
is pointing is a Company
, not a CompanyUser
. If you want to work with the CompanyUser
, the relation is company_visited
, so:
class VisitorSerializer(serializers.ModelSerializer):
visited = CompanyVisitedSerializer(
source='company_visited',
many=True
)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.