Good time of day, community!
Intro: we are new converts to Linux and Python. At the moment practiced pistol for Django. Decided to move away from writing blozhik and write "local air bnb". The point is simple: the admin gets a new apartment, with description, coordinates and images of interiors. The interior connected as inline in the apartment (many-to-one). Also in the interior there is a photo of the interior, with respect to the same many-to-one.
If first save flat, and then create and save the interior, and then naplodili and save the photo, then everything is fine
Problem: But if you try to create a flat and interior (without saving) and load photo and save in the end, the system will first try to save photos (in my case, based on the id of the apartment and interior, which are not yet recorded in the database), then the path will be formed species FlatNone/InteriorNone/...
The options encountered in the vastness:
1)ask a different file names and not to expand the folders. The perfectionist in me resists this decision.
2)Use the presave. And it is up to "the standard" save to save the id and then id of the interior. But it turns out that at the momoent standard save will be one more calls to the database. The same perfectionist on strike. Here is interesting feedback from the community, maybe I pereslegina.
3)there is one more option: to rewrite the save method that would first preserve the flat model, then interior and then a photo. But on this thought I have natyanut trembling knees and I want to see someone's example and hear the Council. Most unlikely to be able to do a normal override.
Yeah, sorry that code in me it's not pretty, sometimes not odenius and kamichu as is, because "it worked!" ;)
I once had to deal with the same problem. But out of laziness decided to follow the principle of Sorl Thumbnail, which generates a path like this:
If you want to treat the perfectionist, then clearly the third option. In practice all use the variant described Altaisoft and not steamed.
Find more questions by tags Django