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
class User(AbstractUser):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
phone = models.CharField(max_length=50, unique=True)
identity_number = models.CharField(max_length=50, unique=True)
address = models.CharField(max_length=200)
city = models.CharField(max_length=200)
post_code = models.CharField(max_length=7, null=True)
visited = models.ManyToManyField(
Company,
through="CompanyUser",
through_fields=(
"user",
"company",
),
)
is_visitor = models.BooleanField("visitor status", default=False)
is_company = models.BooleanField("company status", default=False)
class CompanyUser(models.Model):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
company = models.ForeignKey(
Company, on_delete=models.CASCADE, related_name="company_visitors"
)
user = models.ForeignKey(
User, on_delete=models.CASCADE, related_name="company_visited"
)
checked_in = models.DateTimeField(auto_now_add=False)
checked_out = models.DateTimeField(auto_now=False)
#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: