S
S
SciFi2016-03-13 14:28:10
Django
SciFi, 2016-03-13 14:28:10

How to connect python scripts to django and use its models?

I have several python scripts. Scripts have classes, functions, and all this is scattered across several files.
What is the correct way to use django models in such scripts?
In addition, I will need to hang these scripts on cron. Where is the right place to store them?
I know that there are two ways:
1. Connect django with a direct call:

import os, sys
import django

sys.path.append("...")
#sys.path.append(os.path.realpath(__file__))
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "app.settings")
django.setup()

2. Use custom management commands.
But how to organize the structure correctly and which way is better to choose?
I have this structure: class1 in a separate file, class2 in a separate file, and a file that calls both classes, processes the data received from them, and so on. In both class1 and class2 I need to access django models.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gornostaev, 2016-03-13
@sergey-gornostaev

Django has some pretty specific code organization requirements. It is better to stick to the structure that is described in the documentation.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question