Commits

Nicolás Echániz committed 9075e4b

Added always_update keyword argument for AutoSlugField. With tests

  • Participants
  • Parent commits 379ad77

Comments (0)

Files changed (2)

autoslug/fields.py

         # Set db_index=True unless it's been set manually.
         if 'db_index' not in kwargs:
             kwargs['db_index'] = True
+
+        self.always_update = kwargs.pop('always_update', False)
         super(SlugField, self).__init__(*args, **kwargs)
 
     def pre_save(self, instance, add):
         value = self.value_from_object(instance)
 
         # autopopulate (unless the field is editable and has some value)
-        if self.populate_from and not value: # and not self.editable:
+        if (self.populate_from and not value) or self.always_update: # and not self.editable:
             value = utils.get_prepopulated_value(self, instance)
 
             if __debug__ and not value:

autoslug/tests.py

     date = DateField(blank=True, null=True)
     slug = AutoSlugField(unique_with='date')
 
+
+class ModelWithAutoUpdateEnabled(Model):
+    """
+    >>> a = ModelWithAutoUpdateEnabled(name='My name')
+    >>> a.save()
+    >>> a.slug
+    u'my-name'
+    >>> a.name = 'My new name'
+    >>> a.save()
+    >>> a.slug
+    u'my-new-name'
+    """
+    name = CharField(max_length=200)
+    slug = AutoSlugField(populate_from='name', always_update=True)