profile
viewpoint

Ask questionsUnable to Make existing fields translatable

Hello,

I think I just found a problem in the documentation at: https://django-parler.readthedocs.io

I am trying to translate my model fields and when I try to follow the procedure described in the docs, all I get is: raise TypeError("Translatable model {} does not appear to inherit from TranslatableModel".format(shared_model)) TypeError: Translatable model <class '__fake__.Product'> does not appear to inherit from TranslatableModel

When I try to follow the docs, Python complains that the fields that are in translations = TranslatedFields are duplicated, so the first part of the doc does not work. No idea how to continue at this point, as this is complete road block for me.

Please find below the relevant data classes where I attempt to perform the translations.

from django.db import models
from django.urls import reverse
from parler.models import TranslatableModel, TranslatedFields

class Category(TranslatableModel):

    translations = TranslatedFields(
        name = models.CharField(max_length=200, db_index=True),
        slug = models.SlugField(max_length=200, db_index=True, unique=True))

    class Meta:

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

class Product(TranslatableModel):

    translations = TranslatedFields(
        name = models.CharField(max_length=200, db_index=True),
        slug = models.SlugField(max_length=200, db_index=True),
        description = models.TextField(blank=True)
        )
    category = models.ForeignKey(Category, related_name='products', on_delete=models.CASCADE)
    image = models.ImageField(upload_to='products/%Y/%m/%d', blank=True)
    price = models.DecimalField(max_digits=10, decimal_places=2)
    available = models.BooleanField(default=True)
    created = models.DateTimeField(auto_now_add=True)
    updated = models.DateTimeField(auto_now=True)

    # Commented for model translations.
    # class Meta:
    #
    #     ordering = ('name',)
    #     index_together = (('id', 'slug'),)

    def __str__(self):
        return self.name

    def get_absolute_url(self):
        return reverse('shop:product_detail', args=[self.id, self.slug])

Related admin classes:

from django.contrib import admin
from .models import Category, Product
from parler.admin import TranslatableAdmin

@admin.register(Category)
class CategoryAdmin(TranslatableAdmin):
    list_display = ['name', 'slug']

    def get_prepopulated_fields(self, request, obj=None):
        return {'slug': ('name',)}

@admin.register(Product)
class ProductAdmin(TranslatableAdmin):
    list_display = ['name', 'slug', 'price', 'available', 'created', 'updated']
    list_filter = ['available', 'created', 'updated']

    list_editable = ['price', 'available']

    def get_prepopulated_fields(self, request, obj=None):
        return {'slug': ('name',)}
(venv) 192:myshop Username$ python manage.py makemigrations shop --name "add_translation_model"
Traceback (most recent call last):
  File "/Users/Username/Projects/Python/Django/Django2ByExample/myshop/venv/lib/python3.7/site-packages/parler/models.py", line 942, in contribute_translations
    base = shared_model._parler_meta
AttributeError: type object 'Product' has no attribute '_parler_meta'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "manage.py", line 21, in <module>
    main()
  File "manage.py", line 17, in main
    execute_from_command_line(sys.argv)
  File "/Users/Username/Projects/Python/Django/Django2ByExample/myshop/venv/lib/python3.7/site-packages/django/core/management/__init__.py", line 401, in execute_from_command_line
    utility.execute()
  File "/Users/Username/Projects/Python/Django/Django2ByExample/myshop/venv/lib/python3.7/site-packages/django/core/management/__init__.py", line 395, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/Users/Username/Projects/Python/Django/Django2ByExample/myshop/venv/lib/python3.7/site-packages/django/core/management/base.py", line 328, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/Users/Username/Projects/Python/Django/Django2ByExample/myshop/venv/lib/python3.7/site-packages/django/core/management/base.py", line 369, in execute
    output = self.handle(*args, **options)
  File "/Users/Username/Projects/Python/Django/Django2ByExample/myshop/venv/lib/python3.7/site-packages/django/core/management/base.py", line 83, in wrapped
    res = handle_func(*args, **kwargs)
  File "/Users/Username/Projects/Python/Django/Django2ByExample/myshop/venv/lib/python3.7/site-packages/django/core/management/commands/makemigrations.py", line 168, in handle
    migration_name=self.migration_name,
  File "/Users/Username/Projects/Python/Django/Django2ByExample/myshop/venv/lib/python3.7/site-packages/django/db/migrations/autodetector.py", line 43, in changes
    changes = self._detect_changes(convert_apps, graph)
  File "/Users/Username/Projects/Python/Django/Django2ByExample/myshop/venv/lib/python3.7/site-packages/django/db/migrations/autodetector.py", line 128, in _detect_changes
    self.old_apps = self.from_state.concrete_apps
  File "/Users/Username/Projects/Python/Django/Django2ByExample/myshop/venv/lib/python3.7/site-packages/django/db/migrations/state.py", line 213, in concrete_apps
    self.apps = StateApps(self.real_apps, self.models, ignore_swappable=True)
  File "/Users/Username/Projects/Python/Django/Django2ByExample/myshop/venv/lib/python3.7/site-packages/django/db/migrations/state.py", line 272, in __init__
    self.render_multiple([*models.values(), *self.real_models])
  File "/Users/Username/Projects/Python/Django/Django2ByExample/myshop/venv/lib/python3.7/site-packages/django/db/migrations/state.py", line 307, in render_multiple
    model.render(self)
  File "/Users/Username/Projects/Python/Django/Django2ByExample/myshop/venv/lib/python3.7/site-packages/django/db/migrations/state.py", line 578, in render
    return type(self.name, bases, body)
  File "/Users/Username/Projects/Python/Django/Django2ByExample/myshop/venv/lib/python3.7/site-packages/django/db/models/base.py", line 320, in __new__
    new_class._meta.apps.register_model(new_class._meta.app_label, new_class)
  File "/Users/Username/Projects/Python/Django/Django2ByExample/myshop/venv/lib/python3.7/site-packages/django/db/migrations/state.py", line 338, in register_model
    self.do_pending_operations(model)
  File "/Users/Username/Projects/Python/Django/Django2ByExample/myshop/venv/lib/python3.7/site-packages/django/apps/registry.py", line 424, in do_pending_operations
    function(model)
  File "/Users/Username/Projects/Python/Django/Django2ByExample/myshop/venv/lib/python3.7/site-packages/django/apps/registry.py", line 403, in apply_next_model
    self.lazy_model_operation(next_function, *more_models)
  File "/Users/Username/Projects/Python/Django/Django2ByExample/myshop/venv/lib/python3.7/site-packages/django/apps/registry.py", line 415, in lazy_model_operation
    apply_next_model(model_class)
  File "/Users/Username/Projects/Python/Django/Django2ByExample/myshop/venv/lib/python3.7/site-packages/django/apps/registry.py", line 403, in apply_next_model
    self.lazy_model_operation(next_function, *more_models)
  File "/Users/Username/Projects/Python/Django/Django2ByExample/myshop/venv/lib/python3.7/site-packages/django/apps/registry.py", line 389, in lazy_model_operation
    function()
  File "/Users/Username/Projects/Python/Django/Django2ByExample/myshop/venv/lib/python3.7/site-packages/django/db/models/fields/related.py", line 317, in resolve_related_class
    field.do_related_class(related, model)
  File "/Users/Username/Projects/Python/Django/Django2ByExample/myshop/venv/lib/python3.7/site-packages/django/db/models/fields/related.py", line 388, in do_related_class
    self.contribute_to_related_class(other, self.remote_field)
  File "/Users/Username/Projects/Python/Django/Django2ByExample/myshop/venv/lib/python3.7/site-packages/parler/fields.py", line 58, in contribute_to_related_class
    self.model.contribute_translations(cls)
  File "/Users/Username/Projects/Python/Django/Django2ByExample/myshop/venv/lib/python3.7/site-packages/parler/models.py", line 944, in contribute_translations
    raise TypeError("Translatable model {} does not appear to inherit from TranslatableModel".format(shared_model))
TypeError: Translatable model <class '__fake__.Product'> does not appear to inherit from TranslatableModel
(venv) 192:myshop Username$ 
django-parler/django-parler

Answer questions alzohery

(venv) D:\html\nd\myshop>python manage.py makemigrations shop --name "translations" Migrations for 'shop': shop\migrations\0002_translations.py - Change Meta options on category - Change Meta options on product - Remove field name from category - Remove field slug from category - Alter index_together for product (0 constraint(s)) - Remove field description from product - Remove field name from product - Remove field slug from product - Create model ProductTranslation - Create model CategoryTranslation

useful!

Related questions

No questions were found.
source:https://uonfu.com/
Github User Rank List