aaronvanderlip avatar 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.

Comments (0)

Files changed (2)


         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'


         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, **{field.name: slug})
-        rivals = type(instance).objects.filter(**lookups).exclude(pk=instance.pk)
+        rivals = instance_manager.filter(**lookups).exclude(pk=instance.pk)
         if not rivals:
             # the slug is unique, no model uses it
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.