Commits

Remco Wendt committed 39d7b19

Fixes bug with custom primary key

Comments (0)

Files changed (2)

autoslug/fields.py

                 # raise model.DoesNotExist if current slug is unique
                 rival = model.objects.get(**dict(lookups + ((self.name, slug),) ))
                 # not unique, but maybe the "rival" is the instance itself?
-                if rival.id == instance.id:
+                if rival == instance:
                     raise model.DoesNotExist
                 # the slug is not unique; change once more
                 index += 1

autoslug/tests.py

     name = CharField(max_length=200)
     slug = AutoSlugField(populate_from='name', unique=True)
 
+class ModelWithCustomPrimaryKey(Model):
+    custom_primary_key = CharField(primary_key=True, max_length=1)
+    name = CharField(max_length=200)
+    slug = AutoSlugField(populate_from='name', unique=True)
+
 __doc__ = """
 >>> long_name = 'x' * 250
 >>> foo = Foo(name=long_name)
 >>> bar.save()
 >>> [len(x.slug) for x in Foo.objects.all()]
 [50, 50]
+
+# Models with custom primary keys should work
+>>> first_model_instance = ModelWithCustomPrimaryKey.objects.create(custom_primary_key='a', name='name used in slug')
+>>> second_model_instance = ModelWithCustomPrimaryKey.objects.create(custom_primary_key='b', name='name used in slug')
+
 """