I have a model like this:
class Report(models.Model):
....
incident_categories = models.ManyToManyField(IncidentCategory,
through='IncidentCategoryMembership')
class IncidentCategory(models.Model):
...some properties here
class IncidentCategoryMembership(models.Model):
report=models.ForeignKey(Report, on_delete=models.CASCADE)
incident_category=models.ForeignKey(IncidentCategory,
on_delete=models.CASCADE)
comment=models.CharField(max_length=600)
This is all mapped in a viewset (for Report) that supports POST, PUT,
GET....
The update and create works fine for the nested relationship Report >
IncidentCategoryMembership > IncidentCategory
...basically I can create/update a report that has one or many categories
with a comment...
However I get a problem when I request a report and want it to return me a
report like so {id:1, ....props ,
incident_categories:[incident_category:1, comment:'some comment', ....] ...}
I get this error: 'IncidentCategory' object has no attribute
'incident_category'
serializers....
class ReportSerializer(serializers.ModelSerializer): created_by =
serializers.PrimaryKeyRelatedField(read_only=True,
default=serializers.CurrentUserDefault()) incident_categories =
IncidentCategoryMembershipSerializer(many=True)
class Meta:
model = Report
read_only_fields = ('state',)
exclude = ('locked', 'unlock_requested_by', 'marked_for_deletion_by',)
class IncidentCategoryMembershipSerializer(serializers.ModelSerializer):
incident_category = serializers.PrimaryKeyRelatedField(read_only=True)
class Meta: model = IncidentCategoryMembership fields =
('incident_category', 'comment')
Is there something obvious missing here??
--
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.