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

Fix blank fields trying to insert NULL #110

Merged
merged 3 commits into from
Apr 11, 2024
Merged
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
2 changes: 1 addition & 1 deletion docs/pages/getting-started.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Getting started

class Blog(models.Model):
title = models.CharField(max_length=255)
body = models.TextField(null=True)
body = models.TextField(blank=True)

i18n = TranslationField(fields=("title", "body"))

Expand Down
2 changes: 1 addition & 1 deletion docs/pages/inner-workings.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ The inner workings are illustrated using this model::

class Blog(models.Model):
title = models.CharField(max_length=255)
body = models.TextField(null=True)
body = models.TextField(blank=True)

i18n = TranslationField(fields=("title", "body"))

Expand Down
2 changes: 1 addition & 1 deletion docs/pages/known-issues.rst
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ For example, ``Category`` needs ``objects = MultilingualManager()`` in order to

class Blog(models.Model):
title = models.CharField(max_length=255)
body = models.TextField(null=True)
body = models.TextField(blank=True)
category = models.ForeignKey(Category, null=True, blank=True, on_delete=models.CASCADE)

i18n = TranslationField(fields=("title", "body"))
2 changes: 1 addition & 1 deletion docs/pages/migration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ This is how to migrate from django-modeltranslation (version 0.12.1) to

class Blog(models.Model):
title = models.CharField(max_length=255)
body = models.TextField(null=True)
body = models.TextField(blank=True)

# add this field, containing the TranslationOptions attributes as arguments:
i18n = TranslationField(fields=('title', 'body'), virtual_fields=False)
Expand Down
9 changes: 6 additions & 3 deletions modeltrans/translator.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,10 @@ def add_virtual_fields(Model, fields, required_languages):
# first, add a `<original_field_name>_i18n` virtual field to get the currently
# active translation for a field
field = translated_field_factory(
original_field=original_field, blank=True, null=True, editable=False # disable in admin
original_field=original_field,
blank=True,
null=original_field.null,
editable=False, # disable in admin
)

raise_if_field_exists(Model, field.get_field_name())
Expand All @@ -166,7 +169,7 @@ def add_virtual_fields(Model, fields, required_languages):
original_field=original_field,
language=get_default_language(),
blank=True,
null=True,
null=original_field.null,
editable=False,
)
raise_if_field_exists(Model, field.get_field_name())
Expand All @@ -181,7 +184,7 @@ def add_virtual_fields(Model, fields, required_languages):
original_field=original_field,
language=language,
blank=blank_allowed,
null=blank_allowed,
null=blank_allowed and original_field.null,
)
raise_if_field_exists(Model, field.get_field_name())
field.contribute_to_class(Model, field.get_field_name())
Expand Down
4 changes: 2 additions & 2 deletions tests/app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def __str__(self):

class Blog(models.Model):
title = models.CharField(max_length=255)
body = models.TextField(null=True)
body = models.TextField(blank=True)

category = models.ForeignKey(Category, null=True, blank=True, on_delete=models.CASCADE)
site = models.ForeignKey(Site, null=True, blank=True, on_delete=models.CASCADE)
Expand Down Expand Up @@ -171,7 +171,7 @@ class Challenge(models.Model):
"""Model using a custom fallback language per instance/record."""

title = models.CharField(max_length=255)
header = models.CharField(max_length=255, null=True, blank=True)
header = models.CharField(max_length=255, blank=True)

default_language = models.CharField(
max_length=2, null=True, blank=True, default=get_default_language()
Expand Down
17 changes: 9 additions & 8 deletions tests/test_forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,8 @@ def test_form_initial_values(self):

self.assertEqual(form["title"].initial, initial_data["title"])
self.assertEqual(form["title_fr"].initial, initial_data["title_fr"])
self.assertIsNone(form["header"].initial)
# Empty initial data is always None
self.assertEqual(form["header"].initial, None)
self.assertEqual(form["header_fr"].initial, initial_data["header_fr"])
self.assertEqual(form["default_language"].initial, get_default_language())

Expand All @@ -271,7 +272,7 @@ def test_form_initial_values(self):

self.assertEqual(form["title"].initial, initial_data["title"])
self.assertEqual(form["title_fr"].initial, initial_data["title_fr"])
self.assertIsNone(form["header"].initial)
self.assertEqual(form["header"].initial, "")
self.assertEqual(form["header_fr"].initial, initial_data["header_fr"])
self.assertEqual(form["default_language"].initial, get_default_language())

Expand All @@ -282,15 +283,15 @@ def test_form_valid_and_save(self):
self.assertTrue(form.is_valid())
challenge = form.save()
self.assertEqual(challenge.title, data["title"])
self.assertIsNone(challenge.header)
self.assertEqual(challenge.header, "")

data = {"start_date": "2021-01-01", "end_date": "2021-02-02", "title_fr": "Un title"}
form = ExcludeForm(data=data)
self.assertTrue(form.is_valid())
challenge = form.save()
self.assertEqual(challenge.title, "")
self.assertEqual(challenge.title_fr, data["title_fr"])
self.assertIsNone(challenge.header)
self.assertEqual(challenge.header, "")

with self.subTest("Test that only fallback is required"):
data = {"start_date": "2021-01-01", "end_date": "2021-02-02"}
Expand All @@ -315,9 +316,9 @@ def test_form_valid_and_save(self):
challenge = form.save()
self.assertEqual(challenge.title_nl, data["title_nl"])
self.assertEqual(challenge.title_de, data["title_de"])
self.assertIsNone(challenge.title_fr)
self.assertEqual(challenge.title_fr, "")
self.assertEqual(challenge.title, "")
self.assertIsNone(challenge.header)
self.assertEqual(challenge.header, "")

data = {
"start_date": "2021-01-01",
Expand All @@ -330,9 +331,9 @@ def test_form_valid_and_save(self):
challenge = form.save()
self.assertEqual(challenge.title_nl, data["title_nl"])
self.assertEqual(challenge.title_fr, data["title_fr"])
self.assertIsNone(challenge.title_de)
self.assertEqual(challenge.title_de, "")
self.assertEqual(challenge.title, "")
self.assertIsNone(challenge.header)
self.assertEqual(challenge.header, "")

with self.subTest("Update existing instance"):
challenge = Challenge.objects.create(
Expand Down
Loading