我正在尝试翻译模型类中已有的字段。我在第一步陷入困境,它在TranslatableModel
中继承了Category
类,并添加了TranslatedFields
包装来翻译选定的模型字段。我正在关注一本书'Django by Example'以及django-parler instructions如何做到这一点,但是我收到了以下错误:
File ..../env/lib/python3.5/site-packages/parler/models.py", line 965, in contribute_translations
raise TypeError("The model '{0}' already has a field named '{1}'".format(shared_model.__name__, name))
TypeError: The model 'Category' already has a field named 'name'
在应用django-parler之前:
# models.py
class Category(models.Model):
name = models.CharField(max_length=200,
db_index=True)
slug = models.SlugField(max_length=200,
unique=True)
class Meta:
ordering = ('name',)
verbose_name = 'category'
verbose_name_plural = 'categories'
def __str__(self):
return self.name
def get_absolute_url(self):
return reverse('shop:product_list_by_category',
args=[self.slug])
申请django-parler之后:
# models.py
class Category(TranslatableModel):
name = models.CharField(max_length=200,
db_index=True)
slug = models.SlugField(max_length=200,
unique=True)
translations = TranslatedFields(
name = models.CharField(max_length=200,
db_index=True),
slug = models.SlugField(max_length=200,
unique=True),
)
class Meta:
# ordering = ('name',)
verbose_name = 'category'
verbose_name_plural = 'categories'
def __str__(self):
return self.name
def get_absolute_url(self):
return reverse('shop:product_list_by_category',
args=[self.slug])
答案 0 :(得分:0)
从您的代码判断,您正试图遵循Antonio Mele的“Django by Example”....我遇到了同样的问题。 对我来说,修复是在原始模型中为所有字段添加下划线,因此......
name = models.CharField(max_length = 200,db_index = True)...成为 _name = models.CharField(max_length = 200,db_index = True)
另外一些研究表明,从Django 1.9开始,导入了一个重命名的ReverseSingleRelatedObjectDescriptor。 https://github.com/django-json-api/django-rest-framework-json-api/issues/176
我在以下文件“/Django/local/lib/python2.7/site-packages/parler/models.py”中将导入和另一个条件语句引用(第782行)从“ReverseSingleRelatedObjectDescriptor”更改为“ForwardManyToOneDescriptor” ” .. 但我不确定最后一步是否真的有必要,我认为重命名是足够的 - 祝你好运........