I am writing a script to process some data, and I need to get this data from the database. I write a script not in views.py . The script is in myscript.py . I wanted to import the models, as usual in views.py , but it became mated. I rummaged in the internet, found out that if you need, for example, to work separately with ORM, in a separate file, then you need to prescribe django.setup() . But you just can’t register it as well, there you need to turn on the project settings. Please tell me how to do all this, what to register in order to work normally with ORM Django later.

Here is the project structure:

 [name_project] β”œβ”€β”€ [name_project] β”‚ β”œβ”€β”€ settings β”‚ | β”œβ”€β”€ __init__.py β”‚ | β”œβ”€β”€ _django.py | β”œβ”€β”€ __init__.py β”‚ β”œβ”€β”€ urls.py β”‚ β”œβ”€β”€ wsgi.py β”œβ”€β”€ myapp β”‚ β”œβ”€β”€ models.py β”‚ β”œβ”€β”€ views.py β”‚ β”œβ”€β”€ myscript.py └── manage.py 
  • Documentation . In your case, Settings.py not visible. Probably before django.setup() will help os.environ.setdefault("DJANGO_SETTINGS_MODULE", "name_project.settings._django.py") . Also alternative is m9_psy
  • @ m9_psy did the first thing you wrote. ImportError: No module named 'name_project' . instead of name_project registered the name of your project) - SkiesX
  • 2
    And why do you need your own separate script? Why not add an extra command to the regular Django manage.py via manage.py ? Documentation about this - andreymal
  • @andreymal thanks) it's really easier to do that. - SkiesX

0