question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Error whikle making existing fields translatable - model already has a field

See original GitHub issue

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.

Issue Analytics

  • State:open
  • Created 7 years ago
  • Reactions:6
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

6reactions
vglfrcommented, Jun 12, 2017

Same problem, my workaround:

  • rename names * to *_t inside translations tuple
  • makemigrations
  • manually rename *_t back to * in migration files
2reactions
stefanwcommented, Feb 3, 2021

It looks like this check:

if not isinstance(shared_field, (models.Field, TranslatedFieldDescriptor)):
    raise TypeError("The model '{0}' already has a field named '{1}'".format(shared_model.__name__, name))

does not work with Django 3.1. getattr(shared_model, name) returns a django.db.models.query_utils.DeferredAttribute and thus fails the test for being a model field. The comment above it seems outdated.

           # Currently not allowing to replace existing model fields with translatable fields.
           # That would be a nice feature addition however.

My work around was:

  • prefix existing fields with e.g. _ to create a rename migration
  • add translation fields with original field names
  • create data migration between prefixed fields and translation model
  • remove prefixed fields
Read more comments on GitHub >

github_iconTop Results From Across the Web

Making existing fields translatable(Django) - Stack Overflow
The fix for me was to prefix all fields in the original Model with an ... class Category(TranslatableModel): translations = TranslatedFields( name =...
Read more >
Making existing fields translatable - django-parler
The following guide explains how to make existing fields translatable, and migrate the data from the old fields to translated fields. django-parler stores ......
Read more >
How to create custom model fields - Django documentation
Keep this in mind when creating your own custom fields. The Django Field subclass you write provides the machinery for converting between your...
Read more >
Looker error catalog | Google Cloud
A field referenced by a suggest_dimension parameter does not exist, has been commented out, or is excluded in an Explore by a fields...
Read more >
Enable Data Translation for Custom Fields - Salesforce Help
Allow translation of data stored in custom fields on the Record Alert, Product, and Product Category objects. You can enable data translation on...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found