I’m try to populate data into my db and I’d like to use the django API to do so. I’ve written a python module that allows me to this very easily.
However, within the django shell (manage.py), it doesn’t seem possible to run my “populate.py” file.
Is it possible to run my populate.py from the django shell? Or alternatively, should I instead be running a regular python terminal and importing the necessary django components? Either way, I would greatly appreciate some pointers.
Here is my standard wrapper for running django stuff from the command line. This is also useful if you have cron scripts you want to run.
Update for comment:
The variable mentioned above is not
file, it is__file__(i.e. 2 underscoresfileand 2 more underscores). This is always set by the Python interpreter to the full path of the file it occurs in. As the comment says, if this script lives in a child directory of the main directory, the directory-of-the-directory of the filename gives you the directory name of the main project directory, and adding that tosys.pathwill make sure that all of your imports work correctly. If the script lives in the main directory and you execute it from there, then you don’t need to do that.