Skip to content

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

Open
@an0o0nym

Description

@an0o0nym

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

vglfr commented on Jun 12, 2017

@vglfr

Same problem, my workaround:

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

Rashidov21 commented on Jan 28, 2021

@Rashidov21

is not working with django 3.1

stefanw

stefanw commented on Feb 3, 2021

@stefanw
Contributor

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

h3x4git commented on Sep 27, 2022

@h3x4git

This is still an issue

added a commit that references this issue on Sep 27, 2022
46d08de
h3x4git

h3x4git commented on Sep 27, 2022

@h3x4git

#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

i-salameh95 commented on Aug 31, 2023

@i-salameh95

any update on this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @stefanw@an0o0nym@vglfr@h3x4git@Rashidov21

        Issue actions

          Error whikle making existing fields translatable - model already has a field · Issue #154 · django-parler/django-parler