I am trying to serialize a Many to Many relationship and am getting an
attribution error.
My models.py:
class Person(models.Model):
name = models.CharField(max_length=50)
def __str__(self):
return self.name
class Case(models.Model):
summary = models.TextField()
litigant_count = models.ManyToManyField(Person, through='Litigant')
def __str__(self):
return 'Case %s' % (self.id)
class Litigant(models.Model):
person = models.ForeignKey(Person, on_delete=models.CASCADE,
related_name='people_to_litigant')
case = models.ForeignKey(Case, on_delete=models.CASCADE, related_name=
'case_to_litigant')
role = models.TextField()
My serializers.py
class PersonSerializer(ModelSerializer):
class Meta:
model = Person
fields = ('name')
class LitigantSerializer(ModelSerializer):
class Meta:
model = Litigant
fields = ('person', 'case', 'role')
class CaseSerializer(ModelSerializer):
litigant_count = LitigantSerializer(many=True, read_only=True)
class Meta:
model = Case
fields = ('summary', 'litigant_count')
And my views.py
class PersonViewSet(ModelViewSet):
queryset = Person.objects.all()
serializer_class = PersonSerializer
class LitigantViewSet(ModelViewSet):
queryset = Litigant.objects.all()
serializer_class = LitigantSerializer
class CaseViewSet(ModelViewSet):
queryset = Case.objects.all().prefetch_related('case_to_litigant')
serializer_class = CaseSerializer
However, when I go to the API view for Cases, I receive the following error:
'Person' object has no attribute 'person'
I have checked all over Stackoverflow, posted to Reddit, and searched the
Github repository. No one's advice seems to address this problem. The DRF
documentation for Many to Many relationships is absolutely abhorrent, so
that's no help...
Does anyone know why DRF is sending through an object instead of a
serialized dict?
--
You received this message because you are subscribed to the Google Groups
"Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.