Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django REST-framework Serializer pk field?

What is the pk field in the Serializer class in Django-REST-framework?

I assume that it is the primary key, but is the name 'pk' a reserved term? How does the Serializer class know that is to be the primary key of the Snippet model?

I see no field in the Snippet model named 'pk'.

class SnippetSerializer(serializers.Serializer):
    pk = serializers.Field()  # Note: `Field` is an untyped read-only field.
    title = serializers.CharField(required=False,
                                  max_length=100)
    code = serializers.CharField(widget=widgets.Textarea,
                                 max_length=100000)
    linenos = serializers.BooleanField(required=False)
    language = serializers.ChoiceField(choices=LANGUAGE_CHOICES,
                                       default='python')
    style = serializers.ChoiceField(choices=STYLE_CHOICES,
                                    default='friendly')

....class SnippetSeralizer continues

class Snippet(models.Model):
    created = models.DateTimeField(auto_now_add=True)
    title = models.CharField(max_length=100, blank=True, default='')
    code = models.TextField()
    linenos = models.BooleanField(default=False)
    language = models.CharField(choices=LANGUAGE_CHOICES,
                                default='python',
                                max_length=100)
    style = models.CharField(choices=STYLE_CHOICES,
                             default='friendly',
                             max_length=100)

    class Meta:
        ordering = ('created',)
like image 464
user798719 Avatar asked Mar 14 '13 19:03

user798719


People also ask

What is PK in Django REST framework?

pk is a property that lives on the base Model class in django. db. models : class Model(object): ...

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.

How do you pass extra context data to Serializers in Django REST framework?

In function based views we can pass extra context to serializer with "context" parameter with a dictionary. To access the extra context data inside the serializer we can simply access it with "self. context". From example, to get "exclude_email_list" we just used code 'exclude_email_list = self.

How do I pass Queryset to serializer?

To serialize a queryset or list of objects instead of a single object instance, you should pass the many=True flag when instantiating the serializer. You can then pass a queryset or list of objects to be serialized.


1 Answers

pk is a property that lives on the base Model class in django.db.models:

class Model(object):
    ...
    pk = property(_get_pk_val, _set_pk_val)
    ...

which is used to identify the primary key for the model. I haven't used Django-REST, but they probably just map that to the field on the model.

like image 133
Brandon Avatar answered Sep 20 '22 22:09

Brandon