3
我有一個抽象模型,其中包含我的模型的常見字段,但如何定義一個獨特的slug,因爲我不能在抽象模型上查詢,但只能在其子類上查詢?Django:如何定義抽象模型及其子模塊的獨特子彈?
我在尋找一個乾淨和簡單的方法,無需手動提子類的名稱..
class MainModel(models.Model):
title = models.CharField(_('title'), max_length=150)
slug = models.SlugField(_('slug'), unique=True, max_length=150)
category = models.ForeignKey('Category', verbose_name=_('category'))
class Meta:
abstract = True
def save(self, *args, **kwargs):
# define unique slug for ChildModel1, ChildModel2
class ChildModel1(MainModel):
active = models.BooleanField()
class ChildModel2(MainModel):
content = models.TextField()