Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django-rest-framework update foreign key from id

It's possible to update field by primary key?

Serialzer:

class HuntingDetailViewSerializer(serializers.ModelSerializer):

    species = HuntingSpeciesSerializer(many=True, read_only=True)
    technique = HuntingTechniqueTagSerializer()

    class Meta:
        model = HuntListing
        exclude = ('owner',)

Views:

    listing_id = request.data.get('listing_id')
    listing = HuntListing.objects.get(id=listing_id)

    serializer = HuntingDetailViewSerializer(listing, data=request.data, partial=True)

    if serializer.is_valid():
        serializer.save()

Sample data:

{"listing_id":9, "technique":1, ....}

But i got:

{'technique': {u'non_field_errors': [u'Invalid data. Expected a dictionary, but got int.']}}

If rewrite update and change this parameter to technique_id, i can't see this in validated data:

def update(self, instance, validated_data):
    print(validated_data)
like image 310
Sasha Odegov Avatar asked Jan 04 '16 20:01

Sasha Odegov


Video Answer


1 Answers

The problem is with technique = HuntingTechniqueTagSerializer(). This creates a nested serializer, and so when you try to update the model it's expecting a nested dictionary. If you just remove this line it should work. If you want the nested view, however, you'll have to create separate read/write serializers.

like image 73
Collin Reynolds Avatar answered Jan 03 '23 15:01

Collin Reynolds