аватар question@mail.ru · 01.01.1970 03:00

The dynamic path for Upload_to in the Django model (Models.filefield)

Hello, in Django to version 1.4 there was the opportunity in the model for models.filefield to specify “dynamically” “The created path of storing files in the variable Upload_to

  Models.py #...  attach = models.filefield (Verbose_name = , Blank =  true , upload_to =  lambda  Instance, Filename: %(Instance.  id , filiname)     

d django version 1.4 is like ID is assigned later and therefore Instance.id = None and also Instance.pk = None. Tell me how to point out the ID model on the way.

аватар answer@mail.ru · 01.01.1970 03:00

Your question is a bit at a dead end, because I personally have in version up to 1.4 also ID I was not available in upload_to .

In general, the issue of storing files of files is quite beaten. For me, the most normal option with building hash from timeStamp + the file name and the creation of directory on the first 4m chases of the hash

  Upload_to =  'upload_path/%s/%s/%s.jpg' %( hash  [:  1 ],  hash  [ 2 :  3 ],  hash )  

update regards the ID in the file name. You need to save the file, i.e. After assigning id in the database generate a new path and accordingly move the file along the new path. On my subjective opinion, the operation is too costly.

update2

  import  uuid  def   get_file_path  ( instance, filename ):  ext = filiname.split ( ''. ') [- 1 ]  Filename =  ""%s "" %(uuid.uuid4 (), ext)   retu   'uploads/%s/%s/%s.jpg' %(Filename [:  1 ], Filename [ 2 :    

and in the model

  file = models.filefield (upload_to = get_file_patH, ,  null =  true ,  Blank =  true ,  verbose_name = _ ( u'contact List '))                                       

Latest

Similar