Answer the question
In order to leave comments, you need to log in
How to properly use management\commands in Django?
Let's say I need to create new custom groups in Django. It is suggested on the net to do this by placing the script in management\commands and then executing it: manage.py foo.py
But I can’t understand, how then should all this be done on the prod? It's not migration per se. I can do it by hand.
Answer the question
In order to leave comments, you need to log in
From real projects:
1. init - initializes the project after deployment. This includes filling the database with fixtures, a .csv file parser (because it's easier to put the actual csv than to recreate fixtures), which creates a tree structure of model objects, adds various filters, and much more.
2. warmup_cache - warms up the cache. It runs every minute with a cron, looks at the necessary keys, if their TTL comes to an end, it updates the data and puts it in the cache. The benefit, obviously, is that the cache is not warmed up by user actions, and users who hit the cache miss do not have to wait long.
3. order_windows - starts some "windows" that have not yet started, but already need to. That is, their start_time falls into the minute between the current start of the command and the previous one. In the same way closes "windows". Runs every minute. And no, the status cannot be determined by the current time, because there is a lot of preparatory work going on there: creating objects, sending emails, etc.
4. orders_update_status - closes the order, kicking out those users who joined the order, but did not confirm it. Runs every minute.
In general, everything that cannot be done based on user actions, but only based on time.
Migrations are things that run once. My example - init could be made a migration, but for some reason that I don't remember, I didn't. Most likely because it was possible to do this several times during development and not be afraid to delete / edit data from the "init". By the way, it would be logical to push the execution of this management command into 0001_initial migration.
Migration is not only rebuilding the database structure. The data may also be there. For example, earlier there were groups of users A, B and C, and now we need to add D. Quite a migration. Especially if it is forbidden to do it through the admin panel.
If for some reason this cannot be done through the admin panel, then write a command script like this:
If there is a certain set of groups that should be in the project initially, then add it through fixtures or migrations.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question