Django nani和模型继承

时间:2011-12-05 13:07:29

标签: django inheritance django-models

我使用Django nani http://readthedocs.org/docs/django-nani/en/latest/并想要定义两个模型(一个继承另一个模型):

class ItemBase(TranslatableModel):
    translations = TranslatedFields(
        name = models.CharField(max_length=40)
    )
    slug = models.SlugField(unique=True, max_length=40)
    description = models.CharField(max_length=1000)
    worth = models.PositiveIntegerField()

class EquipBase(ItemBase):
    def __init__(self, *args, **kwargs):
        super(EquipBase, self).__init__(*args, **kwargs)

    level = models.PositiveSmallIntegerField()

但是,使用syncdb时会出现以下错误。

django.core.exceptions.ImproperlyConfigured: No TranslatedFields found on <class 'main.world.items.models.EquipBase'>, subclasses of TranslatableModel must define TranslatedFields.

解决方案是什么?

1 个答案:

答案 0 :(得分:0)

您必须将“翻译”添加到ItemBase的EquipBase中,并使用models.Model作为ItemBase的超类:

class ItemBase(models.Model):
    slug = models.SlugField(unique=True, max_length=40)
    description = models.CharField(max_length=1000)
    worth = models.PositiveIntegerField()



class EquipBase(ItemBase):
    translations = TranslatedFields(
        name = models.CharField(max_length=40)
    )

    def __init__(self, *args, **kwargs):
        super(EquipBase, self).__init__(*args, **kwargs)

    level = models.PositiveSmallIntegerField()