[PYTHON] I want to operate DB using Django's ORM from an external application

For Django model manipulation and references,

python manage.py MY_COMMAND

You can do it, but you have to put it under the Django application as /app_name/management/commands/MY_COMMAND.py, inheritBaseCommand and do as follows.

MY_COMMAND.py


from django.core.management.base import BaseCommand, CommandError
class Command(BaseCommand):
    def handle(self, *args, **options):
        print 'hogefuga'

By doing this, the above manage.py MY_COMMAND can be used as an extension command for Django. The above is fine if you want to create something related to the Django app.

I want to refer to the DB though it has nothing to do with the Django app

If you have a Django DB as the master of some management information, or if you want to use the stored results to link with a completely different program, you want to call it from the outside instead of extending manage.py. ..

Since the back of Django is just SQLite or MySQL, you can access it directly and read the information, but even though you defined Model in Django, you do not use that ORM. So, it's a way to use ORM from the outside.

Load Project as a library

You can also use Django's ORM from outside by following the steps below.

--Specify a Django project in sys.path --Specify project settings.py in Django's environment variable DJANGO_SETTINGS_MODULE --import of models

ORM_test.py


#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import sys

def CallORMapper():
    sys.path.append('/YOUR/DJANGO-PROJECT')
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "DJANGO-PROJECT.settings")
    objects = MODEL_NAME.objects.all()
    from DJANGOAPPNAME.models import MODEL_NAME
    print objects

if __name__ == "__main__":
    CallORMapper()

reference

-Use the Django model from outside the Django project

Recommended Posts

I want to operate DB using Django's ORM from an external application
I want to email from Gmail using Python.
I want to develop an Android application on Android (debugging)
I tried to operate from Postman using Cisco Guest Shell as an API server
Operate Maya from an external Python interpreter using the rpyc module
I want to use an external library with IBM Cloud Functions
I want to automatically operate Chrome on Windows from a virtual machine (ubuntu) started using Vagrant
I want to use jar from python
I want to make an automation program!
Tips for manipulating numpy.ndarray from c ++ -I want to use an iterator-
I want to display an image on Jupyter Notebook using OpenCV (mac)
How to build an application from the cloud using the Django web framework
I want to make a web application using React and Python flask
I want to connect to PostgreSQL from various languages
[Python] I want to manage 7DaysToDie from Discord! 1/3
I want to perform SageMaker inference from PHP
I want to visualize csv files using Vega-Lite!
I want to be an OREMO with setParam!
I want to store DB information in list
I want to make fits from my head
I want to use ceres solver from python
I want to get / execute variables / functions / classes of external files from Python
I want to start over with Django's Migrate
[I want to classify images using Tensorflow] (2) Let's classify images
[Python] I want to manage 7DaysToDie from Discord! 2/3
I want to make C ++ code from Python code!
I want to convert an image to WebP with lollipop
I want to see the file name from DataLoader
I want to detect images of cats from Instagram
I tried to get an AMI using AWS Lambda
I tried to become an Ann Man using OpenCV
[Python3] I want to generate harassment names from Japanese!
I want to automate ssh using the expect command!
I tried to make an OCR application with PySimpleGUI
I want to copy an English paper from pdf and put it in Google Translate
I want to automate ssh using the expect command! part2
Try to operate an Excel file using Python (Pandas / XlsxWriter) ①
Ssh to an external server under http proxy [from Ubuntu 18.04]
[Postgresql] SSH connection to the external DB server from the client
Try to operate an Excel file using Python (Pandas / XlsxWriter) ②
I want to start a lot of processes from python
I want to calculate the allowable downtime from the operating rate
[Go] I want to separate endpoints by Read / Write to DB
I want to convert an ISO-8601 character string to Japan time
I want to install a package from requirements.txt with poetry
I want to send a message from Python to LINE Bot
I tried to get data from AS / 400 quickly using pypyodbc
I want to extract an arbitrary URL from the character string of the html source with python
Note that I was addicted to accessing the DB with Python's mysql.connector using a web application.
I want to solve Sudoku (Sudoku)
I want to automatically find high-quality parts from the videos I shot
Try to operate the database using Python's ORM Peewee (August 2019 version)
I want to make a parameter list from CloudFormation code (yaml)
I tried to make a todo application using bottle with python
When you want to send an object with requests using flask
I want to mess with ALB's default security group from CDK
I tried to get data from AS / 400 quickly using pypyodbc Preparation 1
I want to pass an argument to a python function and execute it from PHP on a web server