Commits

Andy Mikhailenko committed d2099dd

Allow fields listed in unique_with to be empty if they have blank=True.

Comments (0)

Files changed (3)

autoslug/fields.py

 
                 value = getattr(instance, field_name)
                 if not value:
+                    if field.blank:
+                        break
                     raise ValueError('Could not check uniqueness of %s.%s with'
                                      ' respect to %s.%s because the latter is empty.'
                                      ' Please ensure that "%s" is declared *after*'

autoslug/tests.py

     'hello-world_2'
     """
     slug = AutoSlugField(unique=True, sep='_')
+
+
+class ModelWithWrongFieldOrder(Model):
+    """
+    >>> a = ModelWithWrongFieldOrder(slug='test')
+    >>> a.save()
+    Traceback (most recent call last):
+    ...
+    ValueError: Could not check uniqueness of ModelWithWrongFieldOrder.slug with \
+    respect to ModelWithWrongFieldOrder.date because the latter is empty. Please \
+    ensure that "slug" is declared *after* all fields it depends on (i.e. "date"), \
+    and that they are not blank.
+    """
+    slug = AutoSlugField(unique_with='date')
+    date = DateField(blank=False, null=False)
+
+
+class ModelWithAcceptableEmptyDependency(Model):
+    """
+    >>> model = ModelWithAcceptableEmptyDependency
+    >>> instances = [model.objects.create(slug='hello') for x in range(0,2)]
+    >>> [x.slug for x in model.objects.all()]
+    [u'hello', u'hello-2']
+    """
+    date = DateField(blank=True, null=True)
+    slug = AutoSlugField(unique_with='date')
+
 
 setup(
     name     = 'django-autoslug',
-    version  = '1.3.5',
+    version  = '1.3.6',
     packages = ['autoslug'],
 
     requires = ['python (>= 2.4)', 'django (>= 1.0)'],