S
S
shibanovan2019-04-01 11:33:30
Django
shibanovan, 2019-04-01 11:33:30

How to use Django models in your scripts?

Is it possible to somehow access Django models from third-party scripts?
Let's say I have a Card model with a title field
And I want to receive Card.objects.filter(title='Test') from a script on the same server. In this case, the script will simply spin in the background, i.e. called from outside django

Answer the question

In order to leave comments, you need to log in

2 answer(s)
7
776166, 2019-04-01
@shibanovan

In general, they answered from above. You can use commands. It's cool and comfortable. There is a battery for cron.
And if this is a completely external script or something project-site, but related, for example, some kind of chat or bot, then you need to import the django environment and it's better that it spins in the virtual environment of the project.
The import itself looks something like this:

import sys, os
import django
sys.path.append('/path/to/settings/py/')
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
django.setup()

https://docs.djangoproject.com/en/2.1/topics/setti...

A
Alexander, 2019-04-01
@kentuck1213

You can write django command and call it from anywhere

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question