[ACCEPTED]-Use Django ORM as standalone-orm
Ah ok I figured it out and will post the 21 solutions for anyone attempting to do the 20 same thing.
This solution assumes that you 19 want to create new models.
First create a 18 new folder to store your files. We'll call 17 it "standAlone". Within "standAlone", create 16 the following files:
__init__.py
myScript.py
settings.py
Obviously "myScript.py" can 15 be named whatever.
Next, create a directory 14 for your models.
We'll name our model directory 13 "myApp", but realize that this is a normal 12 Django application within a project, as 11 such, name it appropriately to the collection 10 of models you are writing.
Within this directory 9 create 2 files:
__init__.py
models.py
Your going to need a copy 8 of manage.py from an either an existing 7 Django project or you can just grab a copy 6 from your Django install path:
django\conf\project_template\manage.py
Copy the manage.py 5 to your /standAlone directory. Ok so you 4 should now have the following structure:
\standAlone
__init__.py
myScript.py
manage.py
settings.py
\myApp
__init__.py
models.py
Add the following to your myScript.py file:
# settings.py
from django.conf import settings
settings.configure(
DATABASE_ENGINE = "postgresql_psycopg2",
DATABASE_NAME = "myDatabase",
DATABASE_USER = "myUsername",
DATABASE_PASSWORD = "myPassword",
DATABASE_HOST = "localhost",
DATABASE_PORT = "5432",
INSTALLED_APPS = ("myApp")
)
from django.db import models
from myApp.models import *
and add this to your settings.py file:
DATABASE_ENGINE = "postgresql_psycopg2"
DATABASE_NAME = "myDatabase"
DATABASE_USER = "myUsername"
DATABASE_PASSWORD = "myPassword"
DATABASE_HOST = "localhost"
DATABASE_PORT = "5432",
INSTALLED_APPS = ("myApp")
and finally your myApp/models.py:
# myApp/models.py
from django.db import models
class MyModel(models.Model):
field = models.CharField(max_length=255)
and 3 that's it. Now to have Django manage your 2 database, in command prompt navigate to 1 our /standalone directory and run:
manage.py sql MyApp
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.