Django-RESTAPI-tutorial

kimdooh2019·2020년 2월 18일
0

tutorial에서 무엇을 만드는가?어떤 api인가?
code snippet을 만드는 것이다

This tutorial will cover creating a simple pastebin code highlighting Web API. Along the way it will introduce the various components that make up REST framework, and give you a comprehensive understanding of how everything fits together.

https://pastebin.com/

쉽게 이야기해서
언어에 맞게 내장함수들의 색깔을 입혀주는 역할을 함
(아직도 동의하지 못하고 있음)

정렬 sorted 복잡도
Snippet.objects.all
ordering = ['created'] created 기준으로 정렬
정렬!은 비싼 연산임

ordering에 들어간 필드는 인덱스 처리 해준다

인덱스란?
already assign by standard for created

Snippet table Snippet created_date index table
Snippet1 - 20.02.11 2
Snippet2 - 20.02.13 3
Snippet3 - 20.02.05 1

sorted take many resources

Snippet.objects.all()
Snippet3
Snippet1
Snippet2

RelatedField -> index
ForeignKey
ManyToMany
OneToOne

snippets.model.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)
    code = models.TextField()
    # line numbers
    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']
        # DB index settings (Model.Meta)
        indexes = [
            models.Index(fields=['created'])
        ]

profile
testify to the light

0개의 댓글