djangostandalone 0.4

django-standalone 0.4 : Python Package Index

django-standalone 0.4

use the Django ORM with standalone scripts

Downloads ↓

-*- markdown -*-

Simple Standalone Scripts Using the Django ORM
=====================================================

This little library is all about easing the pain of using
the Django ORM for simple tools that just happen to be in need
of some easy to use ORM for object persistence.

The best way of course is to set up a full Django project and
just use a settings.py file and using the DJANGO_SETINGS_MODULE
environment variable. But when you just want to do little tools
that just need some sqlite3 database to store some of their
data and don't want to go for a full Django project, that is
where this little library comes into play.

It consists of just two modules so far:

standalone.conf handles all the configuration needs, setting up

standalone.models carries a base class for your models that
automatically tells Python that all models created actually
reside in the standalone Django app, even though they are
in a different file, outside the app's namespace.

As a warning: this might be seen as voodoo, bad magic or just
a plain stupid idea by some people. And the official way
to do it might be a much better idea for you. I myself just
happen to like to have the ability to easily create standalone
executable scripts that don't rely on some predefined project
structure.

how to get it
---------------

The easiest way to get django-standalone is to use easy_install
or pip:

sudo easy_install django-standalone

or

sudo pip install django-standalone

Or you can clone this repository and run the included setup.py

To run the included test cases, just run the following:

pythons setup.py test

using in your scripts:
------------------------

First create a dynamic configuration for your database
connection:

 from standalone.conf import settings
 settings = settings(
     DATABASE_ENGINE='sqlite3',
     DATABASE_NAME=options.database,
 )

this is all you need to do to have django modules working
in your script. You would have to add additional settings if
you want to use more than just the ORM, for example you will
have to add TEMPLATE_DIR if you want to use the template modules.

You can add any django setting you want - standalone.conf will
allways extend your settings, never overwrite them.

Now you just define a bunch of Models, using the provided base
class in your script. The needed module standalone.models
reexports everything from django.models, so you only need one.

 from standalone import models

 class MyModel(models.StandaloneModel):

     col1 = models.CharField(max_length=1000)
     col2 = models.IntegerField()
     col3 = models.BooleanField()

     def __unicode__(self):
         return self.col1
原文地址:https://www.cnblogs.com/lexus/p/2435603.html