matthiask / django-imagefield

You should probably use this image field instead of Django's built-in models.ImageField.
https://django-imagefield.readthedocs.io
BSD 3-Clause "New" or "Revised" License
102 stars 10 forks source link

MemcachedKeyCharacterError if admin form contains validation errors #3

Closed sacovo closed 4 years ago

sacovo commented 4 years ago

We have a model that uses an imagefield.ImageField and memcached as backend. If this model is:

The form should be rendered again with the errors, but the ImageField tries to get a value from the cache with the invalid key, which leads to a MemcachedKeyCharacterError. This happens because the file name has not been cleaned by django, since the model wasn't saved because of the validation error.

In imagefield/fields.py

            key = "imagefield-admin-thumb:%s" % value.name
            url = cache.get(key, "")

Fix would probably be to catch the exception, or any exception that could come from fetching a value from the cache, and continue with an empty url.