Skip to content

Commit ee25ff7

Browse files
authored
Allow unique_with to have reference to BooleanField (#52)
1 parent 29e9d2e commit ee25ff7

File tree

4 files changed

+30
-1
lines changed

4 files changed

+30
-1
lines changed

AUTHORS.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,5 @@ generally made django-autoslug better:
3939
* Peter Baumgartner
4040
* Jernej Kos
4141
* Sutrisno Efendi
42+
* Jeffrey de Lange
4243
* Your Name Here ;)

autoslug/tests/models.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,12 @@ class ModelWithAcceptableEmptyDependency(Model):
125125
slug = AutoSlugField(unique_with='date')
126126

127127

128+
class ModelWithBooleanInUniqueWith(Model):
129+
name = CharField(max_length=200)
130+
bool = BooleanField()
131+
slug = AutoSlugField(unique_with='bool', populate_from='name')
132+
133+
128134
class ModelWithAutoUpdateEnabled(Model):
129135
name = CharField(max_length=200)
130136
slug = AutoSlugField(populate_from='name', always_update=True)

autoslug/tests/tests.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,28 @@ def test_wrong_field_order(self):
228228
with self.assertRaises(ValueError, msg=errmsg):
229229
a.save()
230230

231+
def test_unique_with_boolean_field(self):
232+
a = ModelWithBooleanInUniqueWith.objects.create(name='test', bool=True)
233+
b = ModelWithBooleanInUniqueWith.objects.create(name='test', bool=False)
234+
c = ModelWithBooleanInUniqueWith.objects.create(name='test', bool=False)
235+
d = ModelWithBooleanInUniqueWith.objects.create(name='test', bool=True)
236+
237+
self.assertEqual(a.slug, 'test')
238+
self.assertEqual(b.slug, 'test')
239+
self.assertEqual(c.slug, 'test-2')
240+
self.assertEqual(d.slug, 'test-2')
241+
242+
def test_unique_with_boolean_field_false_true(self):
243+
a = ModelWithBooleanInUniqueWith.objects.create(name='test', bool=False)
244+
b = ModelWithBooleanInUniqueWith.objects.create(name='test', bool=True)
245+
c = ModelWithBooleanInUniqueWith.objects.create(name='test', bool=False)
246+
d = ModelWithBooleanInUniqueWith.objects.create(name='test', bool=True)
247+
248+
self.assertEqual(a.slug, 'test')
249+
self.assertEqual(b.slug, 'test')
250+
self.assertEqual(c.slug, 'test-2')
251+
self.assertEqual(d.slug, 'test-2')
252+
231253
def test_acceptable_empty_dependency(self):
232254
model = ModelWithAcceptableEmptyDependency
233255
instances = [model.objects.create(slug='hello') for x in range(0,2)]

autoslug/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ def get_uniqueness_lookups(field, instance, unique_with):
115115
% (instance._meta.object_name, field_name))
116116

117117
value = getattr(instance, field_name)
118-
if not value:
118+
if value is not False and not value:
119119
if other_field.blank:
120120
field_object = instance._meta.get_field(field_name)
121121
if isinstance(field_object, ForeignKey):

0 commit comments

Comments
 (0)