Skip to content

Commit

Permalink
fix: Fix update_or_create for Django 4.2
Browse files Browse the repository at this point in the history
Add update_or_create method to MultilingualQuerySet ...

Refs #682, #683
  • Loading branch information
last-partizan committed May 30, 2023
2 parents 4f961bf + e5fb74c commit d5eefa8
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 2 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
python: ["3.8", "3.9", "3.10"]
django: ["3.2", "4.0"]
python: ["3.8", "3.9", "3.10", "3.11"]
django: ["3.2", "4.0", "4.2"]
database: ["sqlite", "postgres", "mysql"]
include:
- python: "3.7"
Expand Down
15 changes: 15 additions & 0 deletions modeltranslation/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,21 @@ def select_related(self, *fields, **kwargs):
new_args.append(rewrite_lookup_key(self.model, key))
return super(MultilingualQuerySet, self).select_related(*new_args, **kwargs)

def update_or_create(self, defaults=None, **kwargs):
"""
Updates or creates a database record with the specified kwargs. The method first
rewrites the keys in the defaults dictionary using a custom function named
`rewrite_lookup_key`. This ensures that the keys are valid for the current model
before calling the inherited update_or_create() method from the super class.
Returns the updated or created model instance.
"""
if defaults is not None:
rewritten_defaults = {}
for key, value in defaults.items():
rewritten_defaults[rewrite_lookup_key(self.model, key)] = value
defaults = rewritten_defaults
return super().update_or_create(defaults=defaults, **kwargs)

# This method was not present in django-linguo
def _rewrite_col(self, col):
"""Django >= 1.7 column name rewriting"""
Expand Down
36 changes: 36 additions & 0 deletions modeltranslation/tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,42 @@ def test_constructor(self):
)
self._test_constructor(keywords)

def test_update_or_create(self):
"""
Test that update_or_create works as expected
"""
# Create new object
obj = models.TestModel.objects.create(title_de='old de', title_en='old en')

# Update existing object
instance, created = models.TestModel.objects.update_or_create(
pk=obj.pk, defaults={'title': 'NEW DE TITLE'}
)
assert created is False
assert instance.title == 'NEW DE TITLE'
assert instance.title_en == 'old en'
assert instance.title_de == 'NEW DE TITLE'

# Check that the translation fields are correctly saved and provide the
# correct value when retrieving them again.
instance.refresh_from_db()
assert instance.title == 'NEW DE TITLE'
assert instance.title_en == 'old en'
assert instance.title_de == 'NEW DE TITLE'

# Create new object
instance, created = models.TestModel.objects.update_or_create(
title_de='old de', title_en='old en'
)

# Check that the translation fields are correctly saved and provide the
# correct value when retrieving them again.
assert created is True
instance.refresh_from_db()
assert instance.title == 'old de'
assert instance.title_en == 'old en'
assert instance.title_de == 'old de'


class ModeltranslationTransactionTest(ModeltranslationTransactionTestBase):
def test_unique_nullable_field(self):
Expand Down

0 comments on commit d5eefa8

Please sign in to comment.