Open
Description
I am trying to translate already existing fields on a model class. I got stuck at the very first step, which is subclassing TranslatableModel
class in Category
class, and adding TranslatedFields
wrapper to translate selected model fields. I am following a book 'Django by Example' as well as the django-parler
instructions on how to do that, however when I run manage.py makemigrations myapp "add_translation_model"
I am getting the following error :
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'
before applying 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])
after applying 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])
I am using Django 1.10.
Activity
vglfr commentedon Jun 12, 2017
Same problem, my workaround:
Rashidov21 commentedon Jan 28, 2021
is not working with django 3.1
stefanw commentedon Feb 3, 2021
It looks like this check:
does not work with Django 3.1.
getattr(shared_model, name)
returns adjango.db.models.query_utils.DeferredAttribute
and thus fails the test for being a model field. The comment above it seems outdated.My work around was:
_
to create a rename migrationh3x4git commentedon Sep 27, 2022
This is still an issue
Fixes django-parler#154
h3x4git commentedon Sep 27, 2022
#324
django.db.models.query_utils.DeferredAttribute
offers access to the.field
from Django 3, so I propose a pull request.Honestly I don't understand under which Django versions this did actually work
i-salameh95 commentedon Aug 31, 2023
any update on this?