抽象类的例子,好深奥,还没想到能用到哪

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):)

你可能感兴趣的:(抽象类的例子,好深奥,还没想到能用到哪)