1👍
Django REST Framework is ignoring the tags
that are being sent in being the tags
field on the IncentiveSerializer
is set to read_only=True
, which means that tags cannot be added, they are only able to be read. Because of this, all of the tags are being ignored and nothing is hitting the database.
You can fix this by setting read_only=False
instead, but this will require you to override create
and update
on the serializer, and DRF 3 does not handle nested serializers by default. The Django REST Framework documentation has some useful information for implementing these methods in a generic way, but it will most likely be specific to the serializer.
class IncentiveSerializer(serializers.ModelSerializer):
tags=TagSerializer(many=True, read_only=False)
class Meta:
model = Incentive
fields = ('schemeName', 'schemeID','text','typeID','typeName','status','ordinal','tags','modeID',
'groupIncentive','condition')
def create(self, validated_data):
tags_data = validated_data.pop("tags", [])
# Ignores tags without a tagId
tags_ids = [tag["tagId"] for tag in tags_data if "tagId" in tag]
incentive = super(IncentiveSerializer, self).create(validated_data)
if tags_ids:
tags = Tag.objects.filter(tagId__in=tags_ids)
incentive.tags.add(*tags)
return incentive
This should allow you to add the tags
when creating the Incentive
, but it requires that the tag already exists and can be found by the tagId
. You may need to change this to meet what you are looking for, as the creation and updating of nested objects is very situation dependent.