Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

id field in django rest framework serializer

Tags:

I'm using django rest framework and just creating a simple serializer like this:

class PackageSerializer(serializers.HyperlinkedModelSerializer):      class Meta:         model = Package         fields = ('id', 'url', 'title','location') 

However I get this error:

KeyError at /cerberus/packages/ 'id' 

How come I can't get the primary key 'id' as part of my serialized data? Thanks for helping!

like image 497
Hieu Phan Avatar asked Feb 21 '13 23:02

Hieu Phan


People also ask

What is serializer method field in Django?

Each field in a Form class is responsible not only for validating data, but also for "cleaning" it — normalizing it to a consistent format. — Django documentation. Serializer fields handle converting between primitive values and internal datatypes.

How do you make a field optional in a serializer?

You could use a SerializerMethodField to either return the field value or None if the field doesn't exist, or you could not use serializers at all and simply write a view that returns the response directly. Update for REST framework 3.0 serializer. fields can be modified on an instantiated serializer.

How does serializer work on Django REST framework?

Serializers in Django REST Framework are responsible for converting objects into data types understandable by javascript and front-end frameworks. Serializers also provide deserialization, allowing parsed data to be converted back into complex types, after first validating the incoming data.

What is Slug related field?

SlugRelatedField may be used to represent the target of the relationship using a field on the target. For example, the following serializer: class AlbumSerializer(serializers. ModelSerializer): tracks = serializers.


2 Answers

HyperlinkedModelSerializer doesn't include the id by default. In 2.2 and earlier you'll need to add it explicitly as a field...

class PackageSerializer(serializers.HyperlinkedModelSerializer):     id = serializers.Field()      class Meta:         model = Package         fields = ('id', 'url', 'title','location') 

From 2.3 onwards, you can simply add 'id' to the fields option...

class PackageSerializer(serializers.HyperlinkedModelSerializer):     class Meta:         model = Package         fields = ('id', 'url', 'title','location') 

From 3.x (< 3.3) onwards, you must use ReadOnlyField() instead of Field() if you want to add it explicitly and not use the fields option...

class PackageSerializer(serializers.HyperlinkedModelSerializer):     id = serializers.ReadOnlyField()      class Meta:         model = Package 
like image 139
Tom Christie Avatar answered Sep 21 '22 15:09

Tom Christie


According to the Django Rest Framework Documentation:

The default implicitly-generated id field is marked as read_only. This causes it to be removed on updates.

Thus, if you need the id field in an update method, you must not declare it as read-only but use a read-write field instead:

class PackageSerializer(serializers.ModelSerializer):     id = serializers.IntegerField() 
like image 44
romor Avatar answered Sep 18 '22 15:09

romor