django model method

dooh kim·2020년 8월 26일
0

django-model-document

목록 보기
3/7

class Ox(models.Model):
    horn_length = models.IntegerField()
    
	class Meta:
        ordering = ['horn_length']
        verbose_name_plural = 'oxen'
        

ordering

뒤에 오는 colum을 기준으로 나열 할 수 있다.

verbose_name_plural
모델 class에 대한 이름 설정

model methods

비지니스 로직을 담는 곳이다!

from django.db import models

class Person(models.Model):
    first_name = models.CharField(max_length=50)
    last_name = models.CharField(max_length=50)
    birth_date = models.DateField()

    def baby_boomer_status(self):
        "Returns the person's baby-boomer status."
        import datetime
        if self.birth_date < datetime.date(1945, 8, 1):
            return "Pre-boomer"
        elif self.birth_date < datetime.date(1965, 1, 1):
            return "Baby boomer"
        else:
            return "Post-boomer"

    @property
    def full_name(self):
        "Returns the person's full name."
        return '%s %s' % (self.first_name, self.last_name)

self.(fieldname) 을 이용해서 method 의 비지니스 로직을 녹여 사용한다.
@property

@property 선언하지 않아도 마치 class의 선언된 attribute 속성처럼 사용 가능하도록 만든다.

@override


class Base():
    def show():
        print('parents class')

class child(Base):
    def show():
        super().show()
        print('child class')
profile
testify to the light

0개의 댓글