0
votes

I have just started leaning Django REST framework step by step in the Tutorial.

In the Tutorial, I declare serializers that work very similar to Django's form in order to provide a way of serializing and deserializing the snippet instances into representation such as json.

Here is snippets/models.py.

from django.db import models
from pygments.lexers import get_all_lexers
from pygments.styles import get_all_styles

LEXERS = [item for item in get_all_lexers() if item[1]]
LANGUAGE_CHOICES = sorted([(item[1][0], item[0]) for item in LEXERS])
STYLE_CHOICES = sorted((item, item) for item in get_all_styles())


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',)

Here is snippets/serializers.py.

from rest_framework import serializers
from snippets.models import Snippet, LANGUAGE_CHOICES, STYLE_CHOICES


class SnippetSerializer(serializers.Serializer):
    id = serializers.IntegerField(read_only=True)
    title = serializers.CharField(required=False, allow_blank=True, max_length=100)
    code = serializers.CharField(style={'base_template': 'textarea.html'})
    linenos = serializers.BooleanField(required=False)
    language = serializers.ChoiceField(choices=LANGUAGE_CHOICES, default='python')
    style = serializers.ChoiceField(choices=STYLE_CHOICES, default='friendly')

    def create(self, validated_data):
        return Snippet.objects.create(**validated_data)

    def update(self, instance, validated_data):
        instance.title = validated_data.get('title', instance.title)
        instance.code = validated_data.get('code', instance.code)
        instance.linenos = validated_data.get('linenos', instance.linenos)
        instance.language = validated_data.get('language', instance.language)
        instance.style = validated_data.get('style', instance.style)
        instance.save()
        return instance

In Django document, I can't find allow_blank, style = {'base_template' : 'textarea.html'} as CharField attribute so that I don't know what they mean.

I would like to know the difference between title = models.CharField(max_length=100, blank=True, default='') in models.py and title = serializers.CharField(required=False, allow_blank=True, max_length=100) in serializers.py.

Please tell me. Thanks for your time.

1

1 Answers

1
votes

Ok, first of all for CharFields do not set both blank and default as True and an empty string. Blank is enough. Blank means that the field is not required and can be empty. Secondly, in your serializer you define absolutely the same fields as in the Model. You can literally remove all that. DRF does that for you, that's the whole point. It just mimics the properties of the fields described in the Model.

You can remove create and update methods too, they are happening automatically.

That's what you actually need.

from rest_framework import serializers
from snippets.models import Snippet

class SnippetSerializer(serializers.ModelSerializer):
    class Meta:
        model = Snippet
        fields = '__all__'