unique_with nullable foreign key

Issue #13 resolved
Bouke Haarsma
created an issue

Say I have an AutoSlugField(populated_from='title', unique_with='some_relation') and some_relation has null=True. Now the following scenario hold:

  1. title='Hello', some_relation=(1), slug becomes hello
  2. title='Hello', some_relation=(2), slug becomes hello
  3. title='Hello', some_relation=None, slug becomes hello-2

The third slug should also hello, as it is unique within all some_relation=None.

Comments (2)

  1. dteskera

    something like this may help in autoslug/utils.py around line 113 change this code

            if not value:
                if other_field.blank:

    to this

            if not value:
                if other_field.blank:
                    from django.db.models import ForeignKey
                    field_object, model, direct, m2m = instance._meta.get_field_by_name(field_name)
                    if isinstance(field_object, ForeignKey):
                        lookup = '%s__isnull' % field_name
                        yield lookup, True
  2. Log in to comment