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