This is Django ORM models
class A(models.Model):
...
class B(models.Model):
a = models.Foreignkey(A, on_delete=models.CASCADE)
class C(models.Model):
a = models.Foreignkey(A, on_delete=models.CASCADE)
And here are the serializers:
class ASerializer(serializers.ModelSerializer):
class Meta:
model = A
class BSerializer(serializers.ModelSerializer):
a = ASerializer(many=False)
class Meta:
model = B
class CSerializer(serializers.ModelSerializer):
a = ASerializer(many=False)
class Meta:
model = C
B and C are working as expected.
Problem 1:
Now, if I want to get data of B and C from serializer A by doing b = BSerializer(many=False)
and c = CSerializer(many=False)
. I will get an error that NameError: name 'BSerializer' is not defined
and if I put B and C serializer above A errors for Aserializer. How do I fix this?
Problem 2:
B and C have one-to-one relation with A. So, when serializing A, it might not have a subsequent B or C or both data in B and C tables. So, in ASerializer
b = BSerializer(many=False)
c = CSerializer(many=False)
might give errors if there is no relation between A and C for a particular row. How do I fix this?