1. 程式人生 > >抽象類的例子,好深奧,還沒想到能用到哪

抽象類的例子,好深奧,還沒想到能用到哪

Relationships defined this way on abstract models are resolved when the model is subclassed as a concrete model and are not relative to the abstract model's app_label:

上面這句沒看懂。

products/models.py

from django.db import models

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

    class Meta:
        abstract = True

production/models.py

from django.db import models
from products.models import AbstractCar

class Manufacturer(models.Model):
    pass

class Car(AbstractCar):
    pass

# Car.manufacturer will point to `production.Manufacturer` here.

Car.manufacturer將指向production.Manufacturer(就是Car上面的class Manufacturer(models.Model):)