Skip to content Skip to sidebar Skip to footer

Create A Nested Serializer With Django Rest Framework, But Without Primary Keys As Identity

I have two models to expose via an API: every RegionValue has a ForeignKey to a MapAnswer. I wish to represent this in our API built using rest_framework by making the RegionValues

Solution 1:

The way I solved this required customising the RegionValueSerializer, intercepting the conversion from native python data types to the field.

classRegionValueSerializer(serializers.ModelSerializer):

    deffield_from_native(self, data, files, field_name, into):
        # We need to check all the data items, and ensure they# are matched to an existing primary id if they already# present# Returns nothing because this method mutates 'into'super(RegionValueSerializer, self).field_from_native(data, files, field_name, into)

        map_answer = self.parent.object
        new_into = []
        for rv in into.get('regionvalue_set'):
            if rv.idisNone:
                try:
                    existing_rv = RegionValue.objects.get(answer=map_answer, region_id=rv.region_id)
                    existing_rv.value = rv.value
                    rv = existing_rv
                except RegionValue.DoesNotExist:
                    pass
            new_into.append(rv)
        into['regionvalue_set'] = new_into

    defget_identity(self, data):
        try:
            # Technically identity is defined by region_id AND self.parent.object.id,# but we assume that RegionValueSerializer will only ever be used as a# field that is part of MapAnswerSerializer.return data.get('region_id', None)
        except AttributeError:
            returnNone

Caveats: Note that some of these methods are not really discussed in rest_framework's docs, so I'm not sure how stable this will be. Also, this solution hits the database more than really necessary (the lookup for existing values is duplicating lookups that occur in the parent Serializer).

Post a Comment for "Create A Nested Serializer With Django Rest Framework, But Without Primary Keys As Identity"