using only the django ORM (DB access model) and nothing else.

News123 news1234 at free.fr
Tue Jun 21 21:10:22 EDT 2011


It seems I found a solution (refer to end of this tessage).

Not sure though if there are any drawbacks or if this method of working
could cause any other issues.


 On 06/22/2011 02:42 AM, News123 wrote:
> On 06/22/2011 01:51 AM, News123 wrote:
>> Hi,
>>
>> I have a small application running on a host without web server and
>> without any need for django except its ORM accessing data bases without
>> explicitely writing sql queries.)
>>
>> I assume there's many libraries (SQL Alchemy or others), which could do
>> this job. and which migh have less overhead than django.
>>
>> As I am already implementing a web server application with django on
>> another host I wanted to use the same syntax / API for my non web
>> application.
>>
>> Now my question:
>>
>> What would be the minimal setup required to use django orms and nothing
>> else.
>>
>>
>> What entries could I remove from settings.py
>> would I still have to add INSATLLED_APPS to the settings or could I just
>> write one script
>>
>> defining only defining settings.DATABASES, and the typical contents of
>> models.py of an application.
>>
>>
>> Thanks in advance  for some suggestions or ideas how you would approach
>> writing a tiny non web application with django.db.models.Models
>>
> I made a very first brute force test:
> 
> settings.py: (only DATABASES is set)
> =======================================
> import os
> MYDIR = os.path.abspath(os.path.dirname(__file__))
> 
> DATABASES = {
>     'default' : {
>         'ENGINE': 'django.db.backends.sqlite3',
>         'NAME': os.path.join(MYDIR, "tiny.db"),
>         'HOST': '',
>         'USER': '',
>         'PASSWORD': '',
>         'PORT': '',
>     }
> }
> 
> 
> myapp.py
> ==========
> #!/usr/bin/env python
> import os
> 
> # just set the env prior to importing a django module
> os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
> from django.db import models
> 
> print "yes this line is executed"
> 
> # this will fail :-(
> class Mini(models.Model):
>     name   = models.CharField(max_length=80)
> 
> 
> 
> 
> ###############################################
> If running myapp.py I get following output:
> 
> yes this line is executed
> Traceback (most recent call last):
>   File "./myapp.py", line 11, in <module>
>     class Mini(models.Model):
>   File
> "/opt/my_python/lib/python2.6/site-packages/Django-1.3-py2.6.egg/django/db/models/base.py",
> line 52, in __new__
>     kwargs = {"app_label": model_module.__name__.split('.')[-2]}
> IndexError: list index out of range
> (my_python)n1234 at mypc:~/minidjango$
> 
> 
> So I need at least a little more to make my script work.
> 


directory structure is now

settings.py
myapp/__init__.py
myapp/models.py
tst.py

settings.py
------------
import os
MYDIR = os.path.abspath(os.path.dirname(__file__))

DATABASES = {
    'default' : {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(MYDIR, "tiny.db"),
        'HOST': '',
        'USER': '',
        'PASSWORD': '',
        'PORT': '',
    }
}

INSTALLED_APPS = (
    'myapp',
)


myapp/models.py
----------------
from django.db import models

class Mini(models.Model):
    name   = models.CharField(max_length=80)


tst.py
-------
import os
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'

import myapp.models as mymodels

for name in ["one", "two", "three"]:
    mymodels.Mini(name=name).save()

print mymodels.Mini.objects.all().values()




now I can call syncdb with:
django-admin syncdb --settings=settings --pythonpath=`pwd`


and run my test app with




More information about the Python-list mailing list