models.py
from pygments.lexers import get_all_lexers
LEXERS = [item for item in get_all_lexers() if item[1]]
('ABAP', ('abap',), ('*.abap', '*.ABAP'), ('text/x-abap',))
('AMDGPU', ('amdgpu',), ('*.isa',), ())
('APL', ('apl',), ('*.apl', '*.aplf', '*.aplo', '*.apln', '*.aplc', '*.apli', '*.dyalog'), ())
........
........
('Zephir', ('zephir',), ('*.zep',), ())
('Zig', ('zig',), ('*.zig',), ('text/zig',))
('ANSYS parametric design language', ('ansys', 'apdl'), ('*.ans',), ())
default
emacs
friendly
friendly_grayscale
........
........
gruvbox-light
dracula
one-dark
lilypond
serializers.py
# 직렬화 & 역직렬화 방법 제공하기
from rest_framework import serializers
from snippets.models import Snippet, LANGUAGE_CHOICES, STYLE_CHOICES
# Serializer class 사용
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):
"""
Create and return a new `Snippet` instance, given the validated data.
"""
return Snippet.objects.create(**validated_data)
def update(self, instance, validated_data):
"""
Update and return an existing `Snippet` instance, given the 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
# ModelSerializer class 사용 -> 직렬 변환기 클래스를 생성하기 위한 클래스
class SnippetSerializer(serializers.ModelSerializer):
class Meta:
model = Snippet
fields = ['id', 'title', 'code', 'linenos', 'language', 'style'] # Snippet 모델의 필드명
Serializer -> ModelSerializer class 변경
serializer가 무얼하는지...정확히 모르겠네..