from django.db import models
class CharField(max_length=None, **options)
# ex
title = models.CharField(max_length=100)
max_length
: 필드의 최대 길이class TextField(**options)
# ex
detail_content = models.TextField()
class IntegerField(**options)
# ex
order_id = models.IntegerField(default=0)
class DecimalField(max_digits=None, decimal_places=None, **options)
# ex
price = models.DecimalField(max_digits=11, decimal_places=2)
max_digits
: 숫자에 허용되는 최대 자릿수. 이 수는 decimal_places보다 크거나 같아야한다.decimal_places
: 숫자와 함께 저장하는 소수 자릿수class FloatField(**options)
class BigIntegerField(**options)
class SmallIntegerField(**options)
# ex
quantity = models.SmallIntegerField()
class PositiveBigIntegerField(**options)
class PositiveIntegerField(**options)
class PositiveSmallIntegerField(**options)
class AutoField(**options)
class BigAutoField(**options)
class SmallAutoField(**options)
class BooleanField(**options)
# ex
is_exit = models.BooleanField(default=False)
class DateField(auto_now=False, auto_now_add=False, **options)
# ex
updated_at = models.DateField(auto_now=True)
created_at = models.DateField(auto_now_add=)
auto_now
auto_now_add
class DateTimeField(auto_now=False, auto_now_add=False, **options)
# ex
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
class EmailField(max_length=254, **options)
class BinaryField(max_length=None, **options)
# ex
my_binary_data = models.BinaryField()
class DurationField(**options)
class FileField(upload_to=None, max_length=100, **options)
class FilePathField(path='', match=None, recursive=False, allow_files=True, allow_folders=False, max_length=100, **options)
class ImageField(upload_to=None, height_field=None, width_field=None, max_length=100, **options)
# ex
photo = models.ImageField(blank=True)
profile_pic = models.ImageField(upload_to="blog/profile_pic")
# 저장경로 : MEDIA_ROOT/blog/profile_pic/xxxx.jpg 경로에 저장
# DB필드 : 'MEDIA_URL/blog/profile_pic/xxxx.jpg' 문자열 저장
photo = models.ImageField(blank=True, upload_to="blog/%Y/%m/%d")
# 저장경로 : MEDIA_ROOT/blog/2017/05/10/xxxx.jpg 경로에 저장
# DB필드 : 'MEDIA_URL/blog/2017/05/10/xxxx.jpg' 문자열 저장
class GenericIPAddressField(protocol='both', unpack_ipv4=False, **options)
class JSONField(encoder=None, decoder=None, **options)
# ex
from django.contrib.postgres.fields import JSONField
from django.db import models
class User(models.Model):
metadata = JSONField()
class Post(models.Model):
data = models.JSONField(default='{}')
class SlugField(max_length=50, **options)
# ex
from django.utils.text import slugify
class Article(models.Model):
headline = models.CharField(max_length=100)
. . .
slug = models.SlugField(unique=True)
def save(self, *args, **kwargs):
self.slug = slugify(self.headline)
super(Article, self).save(*args, **kwargs)
. . .
>>> u1 = User.objects.get(id=1)
>>> from datetime import date
>>> a1 = Article.objects.create(headline="todays market report", pub_date=date(2018, 3, 6), reporter=u1)
>>> a1.save()
# 슬러그는 자동으로 생성됩니다. create 메서드를 따로 정의한 게 아닙니다.
>>> a1.slug
'todays-market-report'
class URLField(max_length=200, **options)
class UUIDField(**options)
class ArrayField(base_field, size=None, **options)[source]
# ex
from django.contrib.postgres.fields import ArrayField
from django.db import models
class ChessBoard(models.Model):
board = ArrayField(
ArrayField(
models.CharField(max_length=10, blank=True),
size=8,
),
size=8,
)
참고사이트
https://django-orm-cookbook-ko.readthedocs.io/en/latest/slugfield.html
https://brunch.co.kr/@ddangdol/4