Package: django-standalone
-*- markdown -*-Simple Standalone Scripts Using the Django ORM=====================================================This little library is all about easing the pain of usingthe Django ORM for simple tools that just happen to be in needof some easy to use ORM for object persistence.The best way of course is to set up a full Django project andjust use a settings.py file and using the DJANGO_SETINGS_MODULEenvironment variable. But when you just want to do little toolsthat just need some sqlite3 database to store some of theirdata and don't want to go for a full Django project, that iswhere this little library comes into play.It consists of just two modules so far:standalone.conf handles all the configuration needs, setting upstandalone.models carries a base class for your models thatautomatically tells Python that all models created actuallyreside in the standalone Django app, even though they arein a different file, outside the app's namespace.As a warning: this might be seen as voodoo, bad magic or justa plain stupid idea by some people. And the official wayto do it might be a much better idea for you. I myself justhappen to like to have the ability to easily create standaloneexecutable scripts that don't rely on some predefined projectstructure.how to get it---------------The easiest way to get django-standalone is to use easy_installor pip:sudo easy_install django-standaloneorsudo pip install django-standaloneOr you can clone this repository and run the included setup.pyTo run the included test cases, just run the following:pythons setup.py testusing in your scripts:------------------------First create a dynamic configuration for your databaseconnection:from standalone.conf import settingssettings = settings(DATABASE_ENGINE='sqlite3',DATABASE_NAME=options.database,)this is all you need to do to have django modules workingin your script. You would have to add additional settings ifyou want to use more than just the ORM, for example you willhave to add TEMPLATE_DIR if you want to use the template modules.You can add any django setting you want - standalone.conf willallways extend your settings, never overwrite them.Now you just define a bunch of Models, using the provided baseclass in your script. The needed module standalone.modelsreexports everything from django.models, so you only need one.from standalone import modelsclass MyModel(models.StandaloneModel):col1 = models.CharField(max_length=1000)col2 = models.IntegerField()col3 = models.BooleanField()def __unicode__(self):return self.col1This will create the model and will make it available directlyin your script. Additionally it will make them available in themodule standalone.models for access by modules you might importin your script or for access in the django shell.This patching into standalone.models only happens for modelsdefined in scripts, though - if your models reside in properpython modules, nothing of that kind will happen, as theyare easily accessible to modules or the shell in their originalmodule. If for any reason you need to force install intostandalone.models, too (for example the testing uses thisto make sure the model is accessible in a standard place eventhough defined in a module), you can add a class variableforce_install_standalone_models with a true value.If you want to access a Django shell with access to your models,you can just use the standard way to access management commandsfrom scripts:from django.core.management import call_commandcall_command("shell")The same way can be used to actually create the tables, too. Putthe following lines directly after your model declarations andyour script will automatically set up the database tables if theydon't yet exist.from django.core.management import call_commandcall_command("syncdb")Using with a library of models--------------------------------You can create library modules with model definitions basedon StandaloneModel, too. These won't be patched into standalone.models,though, so you have to reference them by their own modules. Theycan reference models in your script by using symbolic names.from standalone import modelsclass MyOtherModel(models.StandaloneModel):col1 = models.CharField(max_length=100)col2 = models.ForeignKey("standalone.MyModel")To write a class that again references this library model, just dosomething like the following:from mylibrary.mymodels import MyOtherModelclass AndYetAnotherModel(models.StandaloneModel):col1 = models.ForeignKey(MyOtherModel)There are situations where you need to force a model to be installedinto standalone.models to make sure you can access it from otherplaces. One such situation is setUp methods in test cases. For thatpurpose, you can define an option as follows:class AllwaysInstalledModel(models.StandaloneModel):force_install_standalone_models = Truecol1 = models.CharField(max_length=100)This model will not only reside in it's own scope but will additionallybe hooked up in standalone.models as a global value.CAVEAT: even if a model is not installed into standalone.models,they will allways be regarded as models of the standalone application.Which means, their table names will all start with 'standalone_'in the database! This has to do with the fact that django only likesmodel definitions that are linked to some installed django applicationand django-standalone behaves as an umbrella application for allmodels defined in the context of a script.contacting the author-----------------------Can be done at gb at rfc1437.de in case of problems. Allthough, as thelicense says, essentially it's a "if you break it, you have to keepthe pieces" thing. Of course, I allways like to hear war stories. Justdon't blame me if this little lib kills your production server, huntsdown your boss and eradicates all your companies expense records.