-
-
Notifications
You must be signed in to change notification settings - Fork 105
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Slug is always regenerated #62
Comments
I have solved this issue by updating the class ExtendedAutoSlugField(AutoSlugField):
def pre_save(self, instance, add):
# get currently entered slug
value = self.value_from_object(instance)
if self.always_update or not value:
value = super(ExtendedAutoSlugField, self).pre_save(instance, add)
return value If interested, I can make a merge request. |
I think the problem here is that the name Setting The current assumption in the code seems to be that Example: In from .fields import LongURLField
from .utils import md5_url
class Source(models.Model):
url = LongURLField(null=True, unique=True) # custom url field with max_len > 255
url_md5 = AutoSlugField(populate_from='url', always_update=False, slugify=md5_url, null=True, unique=True)
# more fields here In from hashlib import md5
def md5_url(url):
return md5(url.encode("utf-8")).hexdigest() Application code source, _ = Source.objects.get_or_create(
url_md5=md5_url(url),
defaults={
'url': url,
# more fields here
}
) What should happen is that the existing source is taken from the database if the url already exists, and that a new source is created if the url is new. This didn't work as expected. What happens in the background is this: source.url_md5 = md5('the-website.com')
source.url = 'the-website.com'
source.save() # wrong md5 value (md5 has now been applied twice) For a moment, I was considering a silly workaround: source.url_md5 = md5('the-website.com')
source.url = 'the-website.com'
# (equivalent of adding `'md5_url' : None` to `defaults` in `get_or_create()`)
source.url_md5 = None:
source.save() # correct md5 value
# but
source.save() # wrong md5 value (md5 has now been applied twice)
source.save() # wrong md5 value (md5 has now been applied thrice) So, although I don't want to update the md5 field once it's set, I do have to set
|
Summarizing my previous post: there are actually three scenarios, where
Setting When I started using this library, I expected the choice to be between cases 1 and 2. There may be use cases where option 3 makes sense, e.g. if you want to keep track of the number of saves and the slugify function is a function that adds one to the value. |
Slug is always regenerated when model is saved (ie on update in admin). Even when
always_update=False
.django-autoslug/autoslug/fields.py
Line 275 in 3a87391
The text was updated successfully, but these errors were encountered: