# ...
 class PersonAdvertWizard (SessionWizardView):
     def date_now_to_path (self):
         Return datetime.datetime.now (). strftime ('% Y /% m /% d')
     file_storage = FileSystemStorage (
         location = os.path.join (settings.MEDIA_ROOT, 
                               'photos', 
                               date_now_to_path ()))
     # ...

I want to make so that user-uploaded images are stored in the directories of the form .../photos/Год/Месяц/День .

This location is determined using the file_storage field of the class inherited from the SessionWizardView class.

How to make every time when creating an object of class PersonAdvertWizard field of class file_storage calculated each time in a new way, and the value calculated during import of the module is not used?

    2 answers 2

     from datetime import datetime class A(object): creation = datetime.now() def __init__(self): A.creation = datetime.now() def show(self): print A.creation if __name__ == '__main__': from aa import A a1 = A() a1.show() time.sleep(3) a2 = A() a2.show() a1.show() 

    You can try this

     2016-11-09 22:20:56.947000 2016-11-09 22:20:59.948000 2016-11-09 22:20:59.948000 

      You correctly wrote that file_storage is a class field, and, like a class field, it is initialized once when the class is created (when the module is imported).

      To initialize this field when creating each object, you need to transfer initialization to the constructor, that is, to make this field not an attribute of a class, but an attribute of an object.

       def __init__(self): self.file_storage = FileSystemStorage( ^^^^ location = os.path.join(settings.MEDIA_ROOT, 'photos', self.date_now_to_path())) ^^^^ 
      • No, it does not roll like this: it writes "You need to define 'file_storage' in order to handle file uploads.". We need exactly the class attribute - pynix
      • And how do you refer to it? - Aleksandr Balyunou
      • Why class attribute when creating an object? - Aleksandr Balyunou
      • I do not appeal to him. Appeals to the application formtools (this is Django) - pynix
      • Place the assignment in the class method (working on the class), and pull the method in the constructor - Aleksandr Balyunou