Django抽象父模型保存覆盖

时间:2012-11-18 14:49:30

标签: django django-models slug

我正在尝试在Django中编写一个抽象的父模型,这将帮助我从许多其他子模型的 name 字段中创建一些 slug 字段。它使用 trans 编码,非常适合将西里尔字母转换为拉丁字母。然后它使用django的slugify函数来删除垃圾。

class SlugModel(models.Model):
    class Meta:
        abstract = True

    name = models.CharField(max_length=128, default=u'')
    slug = models.CharField(max_length=128,blank=True)

    def save(self, *args, **kwargs):
        if not self.slug:
            slug = slugify(unicode(self.name).encode('trans'))
        else:
            slug = self.slug
        count = self.__class__.objects.filter(slug = slug).count()
        if count > 1:
            if slug[-2]=='_':
                count = int(slug[-1])
                slug = slug[:-2]
            self.slug = '{0}_{1}'.format(slug,count+1)
        else:
            self.slug = slug
        super(self.__class__, self).save(*args, **kwargs)

    def __unicode__(self):
        return self.name



class Foo(SlugModel):
    pass

当我尝试保存一些Foo对象时会出现问题:它会导致RuntimeError(超出最大递归深度)。我究竟做错了什么?如何正确撰写super(self.__class__, self).save(*args, **kwargs)

2 个答案:

答案 0 :(得分:8)

好的,我明白了。而不是使用super(self.__class__, self).save(*args, **kwargs)

我需要super(SlugModel, self).save(*args, **kwargs)

感谢胡椒种植者。

答案 1 :(得分:0)

只需使用 Error.captureStackTrace(stackObject); ^ page.click: Protocol error (Runtime.awaitPromise): The page has been closed. =========================== logs =========================== waiting for selector ".repo-list-item:nth-child(1) a" selector resolved to visible <a class="v-align-middle" href="/microsoft/playwrig…>…</a> attempting click action waiting for element to be visible, enabled and not moving element is moving - waiting... element is visible, enabled and does not move scrolling into view if needed done scrolling checking that element receives pointer events at (468,222) <div class="mr-3">↵ Apache-2.0 license↵ </div> from <div>…</div> subtree intercepts pointer events retrying click action waiting for element to be visible, enabled and not moving element is moving - waiting... element is visible, enabled and does not move scrolling into view if needed done scrolling checking that element receives pointer events at (468,222) <div class="mr-3">↵ Apache-2.0 license↵ </div> from <div>…</div> subtree intercepts pointer events retrying click action