How to save inline model in the admin, if the id of the model of the parent is not stored in the database?

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!" ;)
With respect.

October 3rd 19 at 01:52
3 answers
October 3rd 19 at 01:54

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:


In other words, each picture gets as the name of the random UUID and displayed on the folders of his first characters. Get a uniform layout for the folders, which eliminates the concern about a possible deceleration of the file system with a large number of files in the folder. Get rid of the need to normalize the file names (as if the Cyrillic characters in the name will fall? all components of the system react to this favorably?). Instead, we lose the correspondence between the structure of the file system and the structure of Django models. PHC, it's irrelevant; maybe You have the situation is different and this method will not work.

I took the file name + take the current time then run it through md5 or sha1 produces a unique name (you need to check that there is no such file, if you have to repeat the procedure). For the directory I used only 2 characters on a directory of the names, depending on the amount of photos you can choose the number of folder levels. PS its Not!! a lot of files in one folder it is harmful to the file system, which then will make unnecessary burden. - lester_OConner95 commented on October 3rd 19 at 01:57
And what is the order for the figures "a lot" in this case? - genevieve.Stehr18 commented on October 3rd 19 at 02:00
I consider myself a normal number of about 10,000 files (100,000 is not very good). You can just test myself =). Ucitaj what folder the same files and folders too, should not be much of a dad. Two characters of the 16 we used the hexadecimal system = 256 combinations and you have 256 folders in a folder. Increasing the nesting you increase the number of dad and increases the number InnoDB ( in the file system and if you are limited, we suggest that not very zealous with folders. - lester_OConner95 commented on October 3rd 19 at 02:03
I think you slept a little) But thank you. And thanks for the link to the inodes.
checked with the Cyrillic alphabet all the rules, but I did Latin+ID-now what heard problems with Cyrillic and special characters in the url to the file. - genevieve.Stehr18 commented on October 3rd 19 at 02:06

def get_photo_path(instance, filename):
 uid = uuid.uuid4().hex
 ext = filename.split('.')[-1]
 while True:
 new_filename = os.path.join('photos', uid[0:2], '%s.%s' % (uid, ext.lower()))
 print new_filename
 if not os.path.exists(os.path.join(settings.MEDIA_ROOT, new_filename)):
 return new_filename
- lester_OConner95 commented on October 3rd 19 at 02:09
But I also would have checked out the ext on acceptable formats for saving. - lester_OConner95 commented on October 3rd 19 at 02:12
October 3rd 19 at 01:56

If you want to treat the perfectionist, then clearly the third option. In practice all use the variant described Altaisoft and not steamed.

October 3rd 19 at 01:58
Thanks for the replies and advice.
2 Altaisoft:
Yeah, I tried to do something similar. UID vilazul the result of hashfunction from time to time + something else. And the names were normalizovat, no Cyrillic characters. But it is not convenient for
1) debug view that sdalat handler photos or JS much fuss over a photo.
2) for the hypothetical support and scalability. A friend who zanimaetsya iOS in moments of debates teaches me that we have to think about his followers - they can fumble Xia for sftp, see the code of the page to do anything else, and very surprised. Besides, this is how it is not nice that the framework can Henrici URLs to pages in chelovekochasov format, but the media files conver blunder.
3)for admins/managerov of just such a hierarchy would be to move the hair in one place. But with them easier, if you really try and finished the admin panel. What I do not frets)

2 nekt:
I really want to. but it is terrible. Don't even know how to approach it. You didn't happen to own any implement such a thing? Think carefully before answering, because then you will not fall behind with questions)

On the weekend one more time googling and if someone will unsubscribe here about the chance to help, I propose to analyze some "just working example". Just on stackoverflow direct the same questions were raised.
To make unique ID can call: uuid.uuid4().hex the uuid from the module and don't need fiddling with time. As for debugging, well, you can write logs with this name. And the JavaScript on the page seems to be not supposed to touch that name... no? Usually work with id's and classes. Right, the safer the system, which is easier. - lester_OConner95 commented on October 3rd 19 at 02:01
How to genitl a unique value for me is not so important, the main thing that it was unique, although it is possible to press that will run faster. - genevieve.Stehr18 commented on October 3rd 19 at 02:04
thank you for the advice. You can log, but this is similar to the judging of prints. Human readable to do better. But I'm almost convinced that I pereslegina.
The only thing left abroad informed that there formset and what makes his save(top answer, and how it differs from the save model (
Hike I have a big problem with the theory. - lester_OConner95 commented on October 3rd 19 at 02:07

Find more questions by tags Django