On Tuesday, 6 September 2016 17:05:39 UTC+1, Tim Graham wrote:
>
> See https://code.djangoproject.com/ticket/4345 for the ticket that
> disallowed primary_key/unique on FileField.
>
I've now opened a new ticket #27188 to suggest reversing this change ;-)
> You can write a custom field to get FileField to use a UUID in the
> database (and likely also lift the other restrictions you want to bypass).
>
Yes, I was wondering how to do that because it's not at all obvious.
However I have since decided that I simply need a custom Storage class like
this:
class ChecksumStorage(FileSystemStorage):
"""Store files in the file system with filename based on file
contents."""
def save(self, name, content, max_length=None):
checksum = hashlib.sha256()
for data in content.chunks():
checksum.update(data)
return super().save(checksum.hexdigest(), content)
and then I don't need a model at all, I can just refer to the files with a
CharField(max_length=64).
--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-users/04f78698-cf07-4a89-b8f0-225d1c5795fa%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.