aaronvanderlip  committed 6f0f390

Added support for using alternate model managers for looking up rival slugs.
This helps support subclassed models that share a common slug space.

  • Participants
  • Parent commits 25e1f90

Comments (0)

Files changed (2)

File autoslug/

         if 'db_index' not in kwargs:
             kwargs['db_index'] = True
+        # When using model inheritence, set manager to search for matching
+        # slug values
+        self.instance_manager = kwargs.pop('instance_manager', None)
         self.always_update = kwargs.pop('always_update', False)
         super(SlugField, self).__init__(*args, **kwargs)
         # get currently entered slug
         value = self.value_from_object(instance)
+        instance_manager = self.instance_manager
         # autopopulate
         if self.always_update or (self.populate_from and not value):
             value = utils.get_prepopulated_value(self, instance)
         # ensure the slug is unique (if required)
         if self.unique or self.unique_with:
-            slug = utils.generate_unique_slug(self, instance, slug)
+            slug = utils.generate_unique_slug(self, instance, slug, instance_manager)
         assert slug, 'value is filled before saving'

File autoslug/

         attr = getattr(instance, field.populate_from)
         return callable(attr) and attr() or attr
-def generate_unique_slug(field, instance, slug):
+def generate_unique_slug(field, instance, slug, instance_manager):
     Generates unique slug by adding a number to given value until no model
     instance can be found with such slug. If ``unique_with`` (a tuple of field
     index = 1
+    if not instance_manager:
+        instance_manager = type(instance).objects
     # keep changing the slug until it is unique
     while True:
         # find instances with same slug
         lookups = dict(default_lookups, **{ slug})
-        rivals = type(instance).objects.filter(**lookups).exclude(
+        rivals = instance_manager.filter(**lookups).exclude(
         if not rivals:
             # the slug is unique, no model uses it