2

我正在尝试翻译模型类上已经存在的字段。我被困在第一步,即在中子类化TranslatableModelCategory,并添加TranslatedFields包装器来翻译选定的模型字段。我正在关注一本书'Django by Example'以及如何做到这一点的django-parler说明,但是我收到以下错误:

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])
4

2 回答 2

0

即使这个问题很老,我也想做出贡献。请添加 django-parler 后的代码片段应如下所示:

  class Category(TranslatableModel):
      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])
于 2022-01-20T16:58:02.607 回答
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”。但是我不确定最后一步是否真的有必要,我认为重命名就足够了??祝你好运.....

于 2018-03-25T16:34:00.027 回答