Skip to content

Commit bd89242

Browse files
committed
Add flag to disable LocalizedUniqueSlugField
1 parent 62e1e80 commit bd89242

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

localized_fields/fields/uniqueslug_field.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ def __init__(self, *args, **kwargs):
3232

3333
kwargs["uniqueness"] = kwargs.pop("uniqueness", get_language_codes())
3434

35+
self.enabled = kwargs.pop("enabled", True)
3536
self.immutable = kwargs.pop("immutable", False)
3637

3738
super(LocalizedUniqueSlugField, self).__init__(*args, **kwargs)
@@ -69,6 +70,9 @@ def pre_save(self, instance, add: bool):
6970
The localized slug that was generated.
7071
"""
7172

73+
if not self.enabled:
74+
return getattr(instance, self.name)
75+
7276
if not isinstance(instance, AtomicSlugRetryMixin):
7377
raise ImproperlyConfigured(
7478
(

tests/test_slug_fields.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import copy
22

3+
import pytest
4+
35
from django import forms
46
from django.conf import settings
57
from django.db import models
@@ -215,6 +217,27 @@ def test_populate_multiple_languages(cls):
215217
for lang_code, lang_name in settings.LANGUAGES:
216218
assert obj.slug.get(lang_code) == "title-%s" % lang_name.lower()
217219

220+
@classmethod
221+
def test_disable(cls):
222+
"""Tests whether disabling auto-slugging works."""
223+
224+
Model = get_fake_model(
225+
{
226+
"title": LocalizedField(),
227+
"slug": LocalizedUniqueSlugField(
228+
populate_from="title", enabled=False
229+
),
230+
}
231+
)
232+
233+
obj = Model()
234+
obj.title = "test"
235+
236+
# should raise IntegrityError because auto-slugging
237+
# is disabled and the slug field is NULL
238+
with pytest.raises(IntegrityError):
239+
obj.save()
240+
218241
@classmethod
219242
def test_allows_override_when_immutable(cls):
220243
"""Tests whether setting a value manually works and does not get

0 commit comments

Comments
 (0)