Use Django ORM as standalone [duplicate]
Asked Answered
T

1

66

Possible Duplicates:
Use only some parts of Django?
Using only the DB part of Django

I want to use the Django ORM as standalone. Despite an hour of searching Google, I'm still left with several questions:

  • Does it require me to set up my Python project with a setting.py, /myApp/ directory, and modules.py file?
  • Can I create a new models.py and run syncdb to have it automatically setup the tables and relationships or can I only use models from existing Django projects?
  • There seems to be a lot of questions regarding PYTHONPATH. If you're not calling existing models is this needed?

I guess the easiest thing would be for someone to just post a basic template or walkthrough of the process, clarifying the organization of the files e.g.:

db/
   __init__.py
   settings.py
   myScript.py
orm/
   __init__.py
   models.py

And the basic essentials:

# settings.py
from django.conf import settings

settings.configure(
     DATABASE_ENGINE   = "postgresql_psycopg2",
     DATABASE_HOST     = "localhost",
     DATABASE_NAME     = "dbName",
     DATABASE_USER     = "user",
     DATABASE_PASSWORD = "pass",
     DATABASE_PORT     = "5432"
)

# orm/models.py
# ...

# myScript.py
# import models..

And whether you need to run something like: django-admin.py inspectdb ...

(Oh, I'm running Windows if that changes anything regarding command-line arguments.).

Tuneful answered 2/6, 2009 at 2:58 Comment(5)
The question linked by William Brendel has the best answer, but also see stackoverflow.com/questions/579511 for some more discussion.Duncandunce
I wonder how you "searched google for 1 hour" and dint get the questions pointed out, by comments by Van Gale and William Brendel.Babbling
its not an "exact duplicate" - while that post helped me to find the answer it wasn't what I was looking for. I hope that closed as "exact duplicate" doesn't change the searchability of my solution below because it is a lot more thorough and how to create models that aren't convered anywhere else.Tuneful
becomingGuru: because all my searches were specific to using Django's ORM. I didn't think to search something as general as how to use parts djangoTuneful
I've created a template Django project that allows you to do just that. github.com/dancaron/Django-ORMUrrutia
T
39

Ah ok I figured it out and will post the solutions for anyone attempting to do the same thing.

This solution assumes that you want to create new models.

First create a new folder to store your files. We'll call it "standAlone". Within "standAlone", create the following files:

__init__.py
myScript.py
settings.py

Obviously "myScript.py" can be named whatever.

Next, create a directory for your models.

We'll name our model directory "myApp", but realize that this is a normal Django application within a project, as such, name it appropriately to the collection of models you are writing.

Within this directory create 2 files:

__init__.py
models.py

Your going to need a copy of manage.py from an either an existing Django project or you can just grab a copy from your Django install path:

django\conf\project_template\manage.py

Copy the manage.py to your /standAlone directory. Ok so you 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 that's it. Now to have Django manage your database, in command prompt navigate to our /standalone directory and run:

manage.py sql MyApp
Tuneful answered 2/6, 2009 at 2:58 Comment(3)
Please update #303151 with new or different information. Please add your answer to that version of your question.Yulandayule
In my case, to need it work (python2.7), I needed to change ("myApp") to ("myApp",), I also needed to append the top level directory to sys.path. In addition, settings.py could be empty (worked for me) - no need to duplicate connection info.Gazzo
What is the point of settings.py if you provide all the same values in your settings.configure() call in myScripy.py?Lamarckism

© 2022 - 2024 — McMap. All rights reserved.