Django Admin - Create 'temporary' entry
Is there a way to make it so the Django Admin panel creates a temporary model when you click the "add" button?
I want to be able to 'attach' multiple files / media to a particular model entry which would involve uploading the files at the time of creation. I can't do this until the model has a pk as obviously I can't create a link between the uploaded file and the entry.
I am using the Content-Type framework to开发者_JS百科 create the attachment between my uploaded file (which is wrapped in a class)
I noticed that Wordpress for example creates what is called an 'auto draft' when you click the "new post" button to get around problem.
If I understand correctly, you want to take care that the filename of your uploaded file corresponds with the model's PK where the file-fields are used.
There is nothing you must change in the django-admin, but make some adjustions on your model:
First, make use of "upload_to" in your filefield. I usually set the filename to a uuid4-value to make sure it's unique.
After saving the model, you can rename the file if you want. The best place is in a function that is triggered by a post-save-signal. But if you only want to ensure, that the filename is unique, the filename-generation by uuid should work.
精彩评论