ulule / django-linguist Goto Github PK
View Code? Open in Web Editor NEWAn application to manage translations in Django models
License: MIT License
An application to manage translations in Django models
License: MIT License
Hi,
I have almost exactly the same configuration as your example and when I try to save a Category model in the admin I am getting the following issue:
IntegrityError: null value in column "name_en" violates not-null constraint
DETAIL: Failing row contains (3, null, null).
Here is my models.py:
class CategoryManager(LinguistManagerMixin, models.Manager):
pass
class Category(with_metaclass(LinguistMeta, models.Model)):
name = models.CharField(
verbose_name=pgettext_lazy('Video category field', 'name'), max_length=255)
objects = CategoryManager()
class Meta:
verbose_name = pgettext_lazy('Video category model', 'category')
verbose_name_plural = pgettext_lazy('Video category model', 'categories')
linguist = {
'identifier': 'myproject_videos_category',
'fields': ('name', ),
}
def __str__(self):
return self.name
def __repr__(self):
return (
'Category(name=%r)' % (
self.name))
Here is my admin.py:
@admin.register(Category)
class CategoryAdmin(TranslatableModelAdmin):
list_display = ('name', 'languages_column', )
Here is the full traceback:
Traceback (most recent call last):
File "/Users/olivier/Documents/PycharmProjects/myproject/lib/python2.7/site-packages/django/core/handlers/exception.py", line 39, in inner
response = get_response(request)
File "/Users/olivier/Documents/PycharmProjects/myproject/lib/python2.7/site-packages/django/core/handlers/base.py", line 187, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/Users/olivier/Documents/PycharmProjects/myproject/lib/python2.7/site-packages/django/core/handlers/base.py", line 185, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Users/olivier/Documents/PycharmProjects/myproject/lib/python2.7/site-packages/django/contrib/admin/options.py", line 544, in wrapper
return self.admin_site.admin_view(view)(*args, **kwargs)
File "/Users/olivier/Documents/PycharmProjects/myproject/lib/python2.7/site-packages/django/utils/decorators.py", line 149, in _wrapped_view
response = view_func(request, *args, **kwargs)
File "/Users/olivier/Documents/PycharmProjects/myproject/lib/python2.7/site-packages/django/views/decorators/cache.py", line 57, in _wrapped_view_func
response = view_func(request, *args, **kwargs)
File "/Users/olivier/Documents/PycharmProjects/myproject/lib/python2.7/site-packages/django/contrib/admin/sites.py", line 211, in inner
return view(request, *args, **kwargs)
File "/Users/olivier/Documents/PycharmProjects/myproject/lib/python2.7/site-packages/django/contrib/admin/options.py", line 1509, in add_view
return self.changeform_view(request, None, form_url, extra_context)
File "/Users/olivier/Documents/PycharmProjects/myproject/lib/python2.7/site-packages/django/utils/decorators.py", line 67, in _wrapper
return bound_func(*args, **kwargs)
File "/Users/olivier/Documents/PycharmProjects/myproject/lib/python2.7/site-packages/django/utils/decorators.py", line 149, in _wrapped_view
response = view_func(request, *args, **kwargs)
File "/Users/olivier/Documents/PycharmProjects/myproject/lib/python2.7/site-packages/django/utils/decorators.py", line 63, in bound_func
return func.get(self, type(self))(*args2, **kwargs2)
File "/Users/olivier/Documents/PycharmProjects/myproject/lib/python2.7/site-packages/django/utils/decorators.py", line 185, in inner
return func(*args, **kwargs)
File "/Users/olivier/Documents/PycharmProjects/myproject/lib/python2.7/site-packages/django/contrib/admin/options.py", line 1449, in changeform_view
self.save_model(request, new_object, form, not add)
File "/Users/olivier/Documents/PycharmProjects/myproject/lib/python2.7/site-packages/django/contrib/admin/options.py", line 1007, in save_model
obj.save()
File "/Users/olivier/Documents/PycharmProjects/myproject/lib/python2.7/site-packages/linguist/mixins.py", line 407, in save
super(ModelMixin, self).save(*args, **kwargs)
File "/Users/olivier/Documents/PycharmProjects/myproject/lib/python2.7/site-packages/django/db/models/base.py", line 796, in save
force_update=force_update, update_fields=update_fields)
File "/Users/olivier/Documents/PycharmProjects/myproject/lib/python2.7/site-packages/django/db/models/base.py", line 824, in save_base
updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
File "/Users/olivier/Documents/PycharmProjects/myproject/lib/python2.7/site-packages/django/db/models/base.py", line 908, in _save_table
result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
File "/Users/olivier/Documents/PycharmProjects/myproject/lib/python2.7/site-packages/django/db/models/base.py", line 947, in _do_insert
using=using, raw=raw)
File "/Users/olivier/Documents/PycharmProjects/myproject/lib/python2.7/site-packages/django/db/models/manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/Users/olivier/Documents/PycharmProjects/myproject/lib/python2.7/site-packages/django/db/models/query.py", line 1045, in _insert
return query.get_compiler(using=using).execute_sql(return_id)
File "/Users/olivier/Documents/PycharmProjects/myproject/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 1054, in execute_sql
cursor.execute(sql, params)
File "/Users/olivier/Documents/PycharmProjects/myproject/lib/python2.7/site-packages/django/db/backends/utils.py", line 79, in execute
return super(CursorDebugWrapper, self).execute(sql, params)
File "/Users/olivier/Documents/PycharmProjects/myproject/lib/python2.7/site-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "/Users/olivier/Documents/PycharmProjects/myproject/lib/python2.7/site-packages/django/db/utils.py", line 94, in exit
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/Users/olivier/Documents/PycharmProjects/myproject/lib/python2.7/site-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
IntegrityError: null value in column "name_en" violates not-null constraint
DETAIL: Failing row contains (3, null, null).
Do you have any idea how I could solve this?
Please tell me if you need more info.
Thanks a lot,
Olivier
Linguist uses a unique identifier string to distinguish the models for which we have translations.
Django's ContentType framework offers such a foreign key right out of the box. Why don't you use that?
My model has a translated field named slug
. If I want to find that object using
MyModel.objects.filter(slug='something')
then I get:
File "django/db/models/sql/query.py", line 1283, in names_to_path
FieldError: Cannot resolve keyword 'slug' into field.
But if I try with MyModel.objects.filter(slug_en='something')
then I get
File "django/db/backends/mysql/base.py", line 295, in quote_name
if name.startswith("`") and name.endswith("`"):
AttributeError: 'NoneType' object has no attribute 'startswith'
The documentation does not mention how to filter on translated fields. Is this implemented?
It looks like django-linguist already supports latest Python and Django versions.
But in pypi, there is only 0.1.3
as final. Could you release a new version?
Getting the following error:
File "/home/project/venv/lib64/python3.11/site-packages/linguist/utils.py", line 10, in <module>
from django.utils.encoding import force_text
ImportError: cannot import name 'force_text' from 'django.utils.encoding' (/home/project/venv/lib64/python3.11/site-packages/django/utils/encoding.py)
I see that a fix for this has been merged to main branch, but a release on PyPi is pending, can you please make a release with the fix!
After setting up my model as:
from django.db import models
from django.utils.six import with_metaclass
from django.utils.translation import ugettext_lazy as _
from linguist.metaclasses import ModelMeta as LinguistMeta
from linguist.mixins import ManagerMixin as LinguistManagerMixin
# Create your models here.
class ProductManager(LinguistManagerMixin, models.Manager):
pass
class Product(with_metaclass(LinguistMeta, models.Model)):
title = models.CharField(max_length=150, default=None)
description = models.TextField(null=True)
short_description = models.TextField(null=True)
ceramic = models.BooleanField(default=None)
painting = models.BooleanField(default=None)
price = models.DecimalField(max_digits=6, decimal_places=2, default=None)
main_image = models.ImageField(upload_to='images/', height_field=None, width_field=None, max_length=100, null=True, blank=True)
objects = PostManager()
class Meta:
verbose_name = "My Product"
linguist = {
'identifier': 'language',
'fields': ('title', 'description','short_description'),
'default_language': 'en',
}
def __str__(self):
return self.title
class Image(models.Model):
product = models.ForeignKey(Product, on_delete=models.CASCADE)
image = models.ImageField(upload_to='images/', height_field=None, width_field=None, max_length=100, null=True, blank=True)
default = models.BooleanField(default=False)
and then migrating my model gives me error:
File "manage.py", line 15, in
execute_from_command_line(sys.argv)
File "C:\Python3\lib\site-packages\django\core\management_init_.py", line 401, in execute_from_command_line
utility.execute()
File "C:\Python3\lib\site-packages\django\core\management_init_.py", line 377, in execute
django.setup()
File "C:\Python3\lib\site-packages\django_init_.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "C:\Python3\lib\site-packages\django\apps\registry.py", line 114, in populate
app_config.import_models()
File "C:\Python3\lib\site-packages\django\apps\config.py", line 211, in import_models
self.models_module = import_module(models_module_name)
File "C:\Python3\Lib\importlib_init_.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "", line 1006, in _gcd_import
File "", line 983, in _find_and_load
File "", line 967, in _find_and_load_unlocked
File "", line 677, in _load_unlocked
File "", line 728, in exec_module
File "", line 219, in _call_with_frames_removed
File "C:\Users\Nadir\dev\zeynab_web\src\products\models.py", line 5, in
from linguist.metaclasses import ModelMeta as LinguistMeta
File "C:\Python3\lib\site-packages\linguist\metaclasses.py", line 4, in
from django.forms.forms import pretty_name
ImportError: cannot import name 'pretty_name' from 'django.forms.forms' (C:\Python3\lib\site-packages\django\forms\forms.py)
Hi,
I am trying to use models.py and admin.py from example.
When I try to create a migration:
python manage.py makemigrations my_app
I see:
Migrations for 'my_app':
my_app/migrations/0001_initial.py
- Create model Bookmark
- Create model BookmarkTranslation
- Create model Category
- Create model Post
and inside I see for example for Bookmark (same for other tables)
('description_en', models.TextField(blank=True)),
('description_ua', models.TextField(blank=True, null=True)),
('description_pl', models.TextField(blank=True, null=True)),
I expected to see no description
fields.
I am using:
UPDATED
It is possible to remove description_* fields by hands.
Getting following error when working with Django 4.0
linguist/utils.py", line 10, in <module>
from django.utils.encoding import force_text
ImportError: cannot import name 'force_text' from 'django.utils.encoding'
Because there is no force_text in Django 4.0 instead use force_str
Solution:
replace
from django.utils.encoding import force_text
to
from django.utils.encoding import force_str
The documentation says to use:
class Post(models.Model):
__metaclass__ = LinguistMeta
...
but this is not compatible with Py2/Py3. Why does the documentation not mention to use the compatibility layer such as:
from django.utils.six import with_metaclass
class Post(with_metaclass(LinguistMeta, models.Model)):
...
I think the migrations aren't up to date. I've tried with Python 2.7 and 3.5. It alters one and two fields respectively:
language
: Only with Python 3.5. The only difference I've found is that language code strings are not prefixed with "b"translation
: In both cases. It adds the following index together ('identifier', 'object_id', 'field_name')
Migration files created
Python 2.7 Django 1.10 - https://gist.github.com/alej0varas/128a3ef3e432912886b895cd374173a9#file-0002_migration_outdated-py
Python 3.5 Django 1.10 - https://gist.github.com/alej0varas/b96b0f174936359d3e9fc24bb9af33d9#file-0002_migration_outdated-py
It looks like the latest pypi release (0.1.3) does not have helpers.py
. This really helps the performance of sites.
There is only 0.5.0 as final on PYPI. Could you release a new version?
Creating a new object works great. However, when updating an existing object, it just keeps the original translation. This happens both in the django admin and the python shell.
Create initial post:
>>> post = Post()
>>> post.activate_language('en')
>>> post.title = 'Hello'
>>> post.save()
>>> post.pk
1
Update post title
>>> post = Post.objects.get(pk=1)
>>> post.activate_language('en')
>>> post.title
Hello
>>> post.title = 'Hello Again'
>>> post.save()
Title is not changed (should be "Hello Again"):
>>> post = Post.objects.get(pk=1)
>>> post.activate_language('en')
>>> post.title
Hello
I'm using django 1.8 and python 2.7.6. I tried this using 0.1.2 from pypi and master.
Updating the translation directly in the linguist.Translation table works.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.