See https://code.djangoproject.com/ticket/4345 for the ticket that 
disallowed primary_key/unique on FileField.

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).

On Sunday, September 4, 2016 at 11:54:06 AM UTC-4, Jon Ribbens wrote:
>
> I have a model which basically just represents a file. I want to use a 
> UUID as the filename. I could do the following:
>
>     def _get_filename(instance, filename):
>         return str(uuid.uuid4())
>
>     class File(Model):
>        content = FileField(db_index=True, upload_to=_get_filename)
>        filetype = CharField("MIME Type", max_length=256)
>
> which does work however it is sub-optimal in a couple of ways:
>
>    - the UUID is stored as a VARCHAR in the database
>    - the UUID is not used as the primary key
>    - there is a pointless additional integer field as the primary key
>    - you can't even add unique=True to the field
>
> So I was wondering:
>
>    - why does FileField refuse to allow its use in a unique index or as 
>    the primary key?
>    - how can I get FileField to use a UUID field in the database?
>
> Any thoughts would be greatly appreciated.
>

-- 
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/d52e4a18-b834-45f7-9d84-1fc6882029f6%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to