Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
an0o0nym opened this issue Aug 31, 2016 · 6 comments

Comments

@an0o0nym
Copy link

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.

@vglfr
Copy link

vglfr commented Jun 12, 2017

Same problem, my workaround:

  • rename names * to *_t inside translations tuple
  • makemigrations
  • manually rename *_t back to * in migration files

@Rashidov21
Copy link

is not working with django 3.1

@stefanw
Copy link
Contributor

stefanw commented 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

@h3x4git
Copy link

h3x4git commented Sep 27, 2022

This is still an issue

h3x4git added a commit to h3x4git/django-parler that referenced this issue Sep 27, 2022
This is a simple fix as @stefanw detected the problem
@h3x4git
Copy link

h3x4git commented 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
Copy link

any update on this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants