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

(UPDATE) prevent of using i18n field as form field #53

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions modeltrans/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ def __init__(self, original_field, language=None, *args, **kwargs):
self.null = kwargs["null"]

self.concrete = False
self.editable = kwargs.pop("editable", True)
self._help_text = kwargs.pop("help_text", None)

@property
Expand Down
17 changes: 3 additions & 14 deletions modeltrans/translator.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from django.core.exceptions import FieldDoesNotExist, ImproperlyConfigured
from django.db.models import Manager

from .conf import get_available_languages, get_default_language
from .conf import get_available_languages
from .fields import TranslationField, translated_field_factory
from .manager import MultilingualManager, transform_translatable_fields

Expand Down Expand Up @@ -153,28 +153,17 @@ def add_virtual_fields(Model, fields, required_languages):
raise_if_field_exists(Model, field.get_field_name())
field.contribute_to_class(Model, field.get_field_name())

# add a virtual field pointing to the original field with name
# <original_field_name>_<LANGUAGE_CODE>
field = translated_field_factory(
original_field=original_field,
language=get_default_language(),
blank=True,
null=True,
editable=False,
)
raise_if_field_exists(Model, field.get_field_name())
field.contribute_to_class(Model, field.get_field_name())

# now, for each language, add a virtual field to get the tranlation for
# that specific langauge
# <original_field_name>_<language>
for language in get_available_languages(include_default=False):
for language in get_available_languages():
blank_allowed = language not in field_required_languages
field = translated_field_factory(
original_field=original_field,
language=language,
blank=blank_allowed,
null=blank_allowed,
editable=original_field.editable,
)
raise_if_field_exists(Model, field.get_field_name())
field.contribute_to_class(Model, field.get_field_name())
Expand Down
35 changes: 6 additions & 29 deletions tests/test_forms.py
Original file line number Diff line number Diff line change
@@ -1,38 +1,15 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.forms import ModelForm
from django.core.exceptions import FieldError
from django.forms import modelform_factory
from django.test import TestCase
from django.utils.translation import override

from .app.models import Blog


class ModelFormTest(TestCase):
def test_modelform(self):
class BlogForm(ModelForm):
class Meta:
model = Blog
fields = ("title_i18n", "body_i18n")

article = Blog(title="English", title_nl="Nederlands")

with override("nl"):
form = BlogForm(
instance=article, data={"title_i18n": "Nederlandse taal", "body_i18n": "foo"}
)
form.save()

article.refresh_from_db()
self.assertEqual(article.title_nl, "Nederlandse taal")
self.assertEqual(article.title_en, "English")

with override("en"):
form = BlogForm(
instance=article, data={"title_i18n": "English language", "body_i18n": "foo"}
)
form.save()

article.refresh_from_db()
self.assertEqual(article.title_nl, "Nederlandse taal")
self.assertEqual(article.title_en, "English language")
def test_i18n_virt_field_modelform(self):
with self.assertRaises(FieldError) as err:
modelform_factory(Blog, fields=("title_i18n",))
self.assertIn("'title_i18n' cannot be specified for Blog", str(err.exception))