ORM๐Ÿน: ์žฅ๊ณ  Abstract Models

# ./products/models.py
from django.db import models


class AbstractCar(models.Model):
    manufacturer = models.ForeignKey('Manufacturer', on_delete=models.CASCADE)

    class Meta:
        abstract = True

๋ฉ”ํƒ€ ํด๋ž˜์Šค์— "abstract = True"์ด๋ฉด migrateํ•˜๋”๋ผ๋„
DB์— ๋ฌผ๋ฆฌํ…Œ์ด๋ธ”์ด ์ƒ๊ธฐ์ง€ ์•Š๋Š”๋‹ค!
์™œ๋ƒ๋ฉด ์ถ”์ƒ ํ…Œ์ด๋ธ”์ด ๋˜์—ˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค!

# ./proudction/models.py
from django.db import models
from products.models import AbstractCar


class Manufacturer(models.Model):
    pass


class Car(AbstractCar):
    pass

์œ„์˜ ๋ชจ๋ธ์„ migrateํ•ด์„œ ์ƒ์„ฑ๋œ ํ…Œ์ด๋ธ”

  • production_car
    CREATE TABLE "production_car" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "manufacturer_id" integer NOT NULL REFERENCES "production_manufacturer" ("id") DEFERRABLE INITIALLY DEFERRED)
    => "production_car" ํ…Œ์ด๋ธ”์€ ์ƒ์†๋ฐ›์€ "AbstractCar"์—์„œ ์ •์˜ ๋œ ์ปฌ๋Ÿผ๊ณผ ๊ฐ™๋‹น!
  • production_manufacturer
    CREATE TABLE "production_manufacturer" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT)
  • ์ฃผ์˜!! product_abstractcar ํ…Œ์ด๋ธ”์€ ์ƒ์„ฑ๋˜์ง€ ์•Š์•˜๋‹ค! ์™œ๋ƒํ•˜๋ฉด AbstractCar๋Š” ์ถ”์ƒํด๋ž˜์Šค๋กœ ์„ค์ •๋˜์–ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค!

Reference

์ข‹์€ ์›นํŽ˜์ด์ง€ ์ฆ๊ฒจ์ฐพ๊ธฐ