Django.fun

Django Rest Framework nested serializers AttributeError 'RelatedManager' object has no attribute

Full Error

Got AttributeError when attempting to get a value for field segments on serializer BackboneLineSerializer. The serializer field might be named incorrectly and not match any attribute or key on the RelatedManager instance. Original exception text was: 'RelatedManager' object has no attribute 'segments'.

I can't figure out why I'm getting this error. I'm using what I think is the exact same code elsewhere with it working. The only thing I can think of is I may have changed the related_name on the backbone_line field of the BackboneLineSegments model from something else to 'segments'. All migrations have been applied, and I don't know why it would be causing issues. Maybe the database doesn't update the new related_name correctly? I'm using Postgres in case it matters.

Models

class BackboneLine(models.Model):
    sub_area = models.ForeignKey(
        SubArea,
        on_delete=models.SET_NULL,
        null=True,
        related_name='backbone_lines')
    source = models.ForeignKey(
        Enclosure,
        on_delete=models.SET_NULL,
        null=True,
        related_name='backbone_lines_out'
    )
    destination = models.OneToOneField(
        Enclosure,
        on_delete=models.SET_NULL,
        null=True,
        related_name='backbone_line_in'
    )

    @property
    def label(self):
        return f'{self.source.box_number} - {self.destination.box_number}'

class BackboneLineSegment(models.Model):
    location = models.LineStringField()
    buried = models.BooleanField(default=False)
    backbone_line = models.ForeignKey(
        BackboneLine,
        on_delete=models.CASCADE,
        related_name='segments'
    )

    @property
    def long_lat(self):
        return [list(coord_pair) for coord_pair in self.location]

    @property
    def lat_long(self):
        return [(cords[1], cords[0]) for cords in self.location.coords]

Serializers

class BackboneLineSegmentSerializer(GeoFeatureModelSerializer):

    class Meta:
        model = BackboneLineSegment
        geo_field = 'location'
        fields = ['location', 'buried']

class BackboneLineSerializer(ModelSerializer):
    segments = BackboneLineSegmentSerializer(many=True)

    class Meta:
        model = BackboneLine
        fields = ['label', 'segments']

Answers: 0