[Django]Model Field

Jimin_Noteยท2022๋…„ 6์›” 11์ผ
0

๐ŸŒฑDjango

๋ชฉ๋ก ๋ณด๊ธฐ
11/13
post-thumbnail

๐Ÿ“field type

from django.db import models

โœ๏ธ String Field

CharField

class CharField(max_length=None, **options)

# ex
name = models.CharField(max_length=20)

ํ•„์ˆ˜ ์ธ์ž

  • max_length : ํ•„๋“œ์˜ ์ตœ๋Œ€ ๊ธธ์ด

TextField

ํ…์ŠคํŠธ ์–‘์ด ๋งŽ์€ ๊ฒฝ์šฐ

class TextField(**options)

# ex
description= models.TextField()

โœ๏ธNumeric Field

IntegerField

4 Bytes๊นŒ์ง€ ์ •์ˆ˜ ๊ฐ’

class IntegerField(**options)

# ex
order_id = models.IntegerField(default=0)

DecimalField

๊ณ ์ • ์†Œ์ˆ˜์  ์ดํ•˜์˜ ์‹ญ์ง„์ˆ˜

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 : ์ˆซ์ž์™€ ํ•จ๊ป˜ ์ €์žฅํ•˜๋Š” ์†Œ์ˆ˜ ์ž๋ฆฟ์ˆ˜
    ์†Œ์ˆ˜์  ์ดํ•˜ 3 ์ž๋ฆฌ์ˆ˜์˜ ์ˆซ์ž๋ฅผ 999๊ฐœ๊นŒ์ง€ ์ €์žฅํ•˜๋ ค๋ฉด max_digits=5 ๋ฐ decimal_places=3 ์‚ฌ์šฉ

FloatField

class FloatField(**options)

BigIntegerField

8 Bytes๊นŒ์ง€ ์ •์ˆ˜ ๊ฐ’

class BigIntegerField(**options)

SmallIntegerField

2 Bytes๊นŒ์ง€ ์ •์ˆ˜ ๊ฐ’

class SmallIntegerField(**options)

# ex
quantity = models.SmallIntegerField()

PositiveIntegerField

class PositiveIntegerField(**options)

PositiveBigIntegerField

class PositiveBigIntegerField(**options)

PositiveSmallIntegerField

class PositiveSmallIntegerField(**options)

AutoField

์ž๋™์œผ๋กœ ์ฆ๊ฐ€ํ•˜๋Š” ์ •์ˆ˜ ํ•„๋“œ

class AutoField(**options)

BigAutoField

1 to 9223372036854775807 ๊นŒ์ง€ 1์”ฉ ์ž๋™ ์ฆ๊ฐ€

class BigAutoField(**options)

SmallAutoField

1 to 32767๊นŒ์ง€ 1์”ฉ ์ž๋™ ์ฆ๊ฐ€

class SmallAutoField(**options)

BooleanField

true/false ํ•„๋“œ
default๊ฐ€ ์ •ํ•ด์žˆ์ง€ ์•Š์€ ๊ฒฝ์šฐ default๊ฐ’์€ None

class BooleanField(**options)

# ex
is_exit = models.BooleanField(default=False)

โœ๏ธDateTime

DateField

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
๊ฐ์ฒด๊ฐ€ ์ €์žฅ๋  ๋•Œ ํ˜„์žฌ ๋‚ ์งœ๋ฅผ ์ €์žฅ
Model.save()๊ฐ€ ๋  ๋•Œ ์ž๋™์œผ๋กœ ์—…๋ฐ์ดํŠธ ๋˜๊ณ , QuerySet.update()๊ฐ™์€ ๊ฒฝ์šฐ์—๋Š” ์—…๋ฐ์ดํŠธ ๋˜์ง€ ์•Š๋Š”๋‹ค.

auto_now_add
๊ฐ์ฒด๊ฐ€ ์ฒ˜์Œ ์ €์žฅ๋  ๋•Œ ํ˜„์žฌ ๋‚ ์งœ๋ฅผ ์ €์žฅ

auto_now_add, auto_now, default ์˜ต์…˜์€ ์ƒํ˜ธ ๋ฐฐํƒ€์ ์ด๋ผ, ์ด๋Ÿฌํ•œ ์˜ต์…˜์„ ์กฐํ•ฉํ•˜๋ฉด ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.

DateTimeField

DateField + ์‹œ๊ฐ„๊ฐ€์ง€ ์ €์žฅ

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)

EmailField

class EmailField(max_length=254, **options)

EmailValidator๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์œ ํšจํ•œ ๋ฉ”์ผ์ฃผ์†Œ์ธ์ง€ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

BinaryField

๋ฐ”์ด๋„ˆ๋ฆฌ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋˜๋Š” ํŠน์ˆ˜ ํ•„๋“œ
๋ฐ”์ดํŠธ๋งŒ ํ—ˆ์šฉ
๋ฐ์ดํ„ฐ๋Š” ์ €์žฅ์‹œ serialize๋œ๋‹ค.
๋ฐ”์ด๋„ˆ๋ฆฌ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•˜๋ฏ€๋กœ์ด ํ•„๋“œ๋Š” ํ•„ํ„ฐ์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๋‹ค.

class BinaryField(max_length=None, **options)

# ex
my_binary_data = models.BinaryField()

DurationField

๊ธฐ๊ฐ„์„ ์ €์žฅํ•˜๊ธฐ์œ„ํ•œ ํ•„๋“œ

class DurationField(**options)

FileField

class FileField(upload_to=None, max_length=100, **options)

ํŒŒ์ผ ์—…๋กœ๋“œ ํ•„๋“œ
primary_key ๋งค๊ฐœ๋ณ€์ˆ˜๋Š” ์ง€์› โŒ

FilePathField

ํŒŒ์ผ์‹œ์Šคํ…œ์—์„œ ํŠน์ •ํ•œ ๋””๋ ‰ํ† ๋ฆฌ์— ํŒŒ์ผ ์ด๋ฆ„์œผ๋กœ ์ œํ•œํ•œ๋‹ค.
์„ธ๊ฐ€์ง€ ํŠน๋ณ„์ธ์ž๊ฐ€ ์žˆ๊ณ  ์ฒซ๋ฒˆ์งธ ์ธ์ž๋Š” ํ•„์ˆ˜์ด๋‹ค.

class FilePathField(path='', match=None, recursive=False, allow_files=True, allow_folders=False, max_length=100, **options)

ํ•„์ˆ˜ ์ธ์žpath
FilePathField๊ฐ€ ์„ ํƒํ•ด์•ผํ•˜๋Š” ๋””๋ ‰ํ† ๋ฆฌ๋ฅผ ์œ„ํ•œ ์ ˆ๋Œ€ ๊ฒฝ๋กœ
ex) "home/imgs"
match
ํŒŒ์ผ์ด๋ฆ„์„ ํ•„ํ„ฐ๋งํ•  ๋•Œ ์‚ฌ์šฉํ•  ๋ฌธ์ž์—ด๋กœ ๋œ ์ •๊ทœํ‘œํ˜„์‹
์ •๊ทœํ‘œํ˜„์‹์€ ์ „์ฒด ๊ฒฝ๋กœ๊ฐ€ ์•„๋‹Œ ๊ธฐ๋ณธ ํŒŒ์ผ์ด๋ฆ„์— ์ ์šฉ
recursive
True/False (default = False)
path์˜ ๋ชจ๋“  ์„œ๋ธŒ ๋””๋ ‰ํ† ๋ฆฌ๊ฐ€ ํฌํ•จ๋˜์–ด์•ผํ•˜๋Š”์ง€ ์—ฌ๋ถ€
allow_files
True/False (default = True)
์ง€์ •๋œ ์œ„์น˜์— ํŒŒ์ผ์„ ํฌํ•จํ• ์ง€ ์—ฌ๋ถ€
allow_files, allow_folders ๋‘˜ ์ค‘ ํ•˜๋‚˜๋Š” True์—ฌ์•ผ ํ•œ๋‹ค.
allow_folders
True/False (default = True)
์ง€์ •๋œ ์œ„์น˜์— ํด๋”๋ฅผ ํฌํ•จํ• ์ง€ ์—ฌ๋ถ€

ImageField

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="jimin/profile_pic")
# ์ €์žฅ๊ฒฝ๋กœ : `ROOT/jimin/profile_pic/xxxx.jpg` ๊ฒฝ๋กœ์— ์ €์žฅ
# DBํ•„๋“œ : `URL/jimin/profile_pic/xxxx.jpg` ๋ฌธ์ž์—ด ์ €์žฅ

photo = models.ImageField(blank=True, upload_to="blog/%Y/%m/%d")
# ์ €์žฅ๊ฒฝ๋กœ : `ROOT/jimin/2022/01/05/xxxx.jpg` ๊ฒฝ๋กœ์— ์ €์žฅ
# DBํ•„๋“œ : `URL/jimin/2022/01/05/xxxx.jpg` ๋ฌธ์ž์—ด ์ €์žฅ

์ด๋ฏธ์ง€ ์ €์žฅ์„ ์ง€์›ํ•˜๋Š” ๋ชจ๋ธ ํ•„๋“œ (FileField ์ƒ์†)

GenericIPAddressField

๋ฌธ์ž์—ด ํ˜•์‹์— IPv4๋‚˜ IPv6 ์ฃผ์†Œ

class GenericIPAddressField(protocol='both', unpack_ipv4=False, **options)

JSONField

JSON ์ธ์ฝ”๋”ฉ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•˜๊ธฐ์œ„ํ•œ ํ•„๋“œ

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='{}')

SlugField

์˜๋ฏธ์žˆ๋Š” URL์„ ๋งŒ๋“œ๋Š”๋ฐ ์‚ฌ์šฉ

???

URLField

URL์„ ์ €์žฅํ•˜๊ธฐ ์œ„ํ•œ CharField์ด๋‹ค.
URLValidator๋ฅผ ์ด์šฉํ•˜์—ฌ ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ๋ฅผ ํ•  ์ˆ˜ ์žˆ๋‹ค.

class URLField(max_length=200, **options)

UUIDField

๊ณ ์œ ํ•œ ์‹๋ณ„์ž๋ฅผ ์ €์žฅํ•˜๊ธฐ ์œ„ํ•œ ํ•„๋“œ

class UUIDField(**options)

ArrayField

profile
Hello. I'm jimin:)

0๊ฐœ์˜ ๋Œ“๊ธ€