我想在save()方法中获取模型的名称。我想纠正这一行
self.slug = create_slug(self.model_object, self.slug)
这里我需要获取模型名称self.model_name
。此处model course
继承了basemodel
,但我需要将model
名称设为course
。我怎样才能做到这一点?有人请帮助我。
class BaseModel(models.Model):
slug = models.SlugField(unique=True, max_length=500)
def save(self, *args, **kwargs):
if self._state.adding == True:
if not self.slug:
self.slug = create_slug(self.model_object, self.slug)
return super(BaseModel, self).save(*args, **kwargs)
class Meta:
abstract = True
class Course(BaseModel):
title = models.CharField(max_length=128)
class Meta:
db_table = "course"
答案 0 :(得分:1)
您可以使用__class__
魔法属性
self.slug = create_slug(self.__class__, self.slug)