Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #8145 > unrolled thread

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

Started byNews123 <news1234@free.fr>
First post2011-06-22 01:51 +0200
Last post2011-06-22 03:16 +0200
Articles 7 — 3 participants

Back to article view | Back to comp.lang.python


Contents

  using only the django ORM (DB access model) and nothing else. News123 <news1234@free.fr> - 2011-06-22 01:51 +0200
    Re: using only the django ORM (DB access model) and nothing else. News123 <news1234@free.fr> - 2011-06-22 02:42 +0200
      Re: using only the django ORM (DB access model) and nothing else. Ian Kelly <ian.g.kelly@gmail.com> - 2011-06-21 19:04 -0600
        Re: using only the django ORM (DB access model) and nothing else. News123 <news1234@free.fr> - 2011-06-22 03:14 +0200
      Re: using only the django ORM (DB access model) and nothing else. News123 <news1234@free.fr> - 2011-06-22 03:10 +0200
    Re: using only the django ORM (DB access model) and nothing else. Roy Smith <roy@panix.com> - 2011-06-21 21:02 -0400
      Re: using only the django ORM (DB access model) and nothing else. News123 <news1234@free.fr> - 2011-06-22 03:16 +0200

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

FromNews123 <news1234@free.fr>
Date2011-06-22 01:51 +0200
Subjectusing only the django ORM (DB access model) and nothing else.
Message-ID<4e012e8d$0$23682$426a34cc@news.free.fr>
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

[toc] | [next] | [standalone]


#8152

FromNews123 <news1234@free.fr>
Date2011-06-22 02:42 +0200
Message-ID<4e013a7b$0$26173$426a74cc@news.free.fr>
In reply to#8145
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@mypc:~/minidjango$


So I need at least a little more to make my script work.









[toc] | [prev] | [next] | [standalone]


#8156

FromIan Kelly <ian.g.kelly@gmail.com>
Date2011-06-21 19:04 -0600
Message-ID<mailman.259.1308704678.1164.python-list@python.org>
In reply to#8152
On Tue, Jun 21, 2011 at 6:42 PM, News123 <news1234@free.fr> wrote:
> ###############################################
> 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@mypc:~/minidjango$
>
>
> So I need at least a little more to make my script work.

There's a bit of magic in the way Django finds things, and I think
you'll still need to keep the basic structure of a Django project --
models should be in a "models.py" file located in an "app" package,
which should be included in the INSTALLED_APPS setting.  You just
won't have any views or urlconfs or templates or admin sites or
anything like that.

[toc] | [prev] | [next] | [standalone]


#8159

FromNews123 <news1234@free.fr>
Date2011-06-22 03:14 +0200
Message-ID<4e0141ee$0$4138$426a74cc@news.free.fr>
In reply to#8156
On 06/22/2011 03:04 AM, Ian Kelly wrote:
>>
>> So I need at least a little more to make my script work.
> 
> There's a bit of magic in the way Django finds things, and I think
> you'll still need to keep the basic structure of a Django project --
> models should be in a "models.py" file located in an "app" package,
> which should be included in the INSTALLED_APPS setting.  You just
> won't have any views or urlconfs or templates or admin sites or
> anything like that.

Hi Ian,

Thanks for your answer.
Ourt messages crossed. I had exactly the same idea and started playing..
and you are right.

The settings module needs only
 DATABASES
and INSTALLED_APPS with one app

and in the apps dir I need apart from the compulsory __init__.py only
models.py

Cool





[toc] | [prev] | [next] | [standalone]


#8158

FromNews123 <news1234@free.fr>
Date2011-06-22 03:10 +0200
Message-ID<4e0140fe$0$4389$426a74cc@news.free.fr>
In reply to#8152
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@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

[toc] | [prev] | [next] | [standalone]


#8155

FromRoy Smith <roy@panix.com>
Date2011-06-21 21:02 -0400
Message-ID<roy-00B024.21021121062011@news.panix.com>
In reply to#8145
In article <4e012e8d$0$23682$426a34cc@news.free.fr>,
 News123 <news1234@free.fr> 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.)

You would do much better to ask this question on the django mailing list 
(http://groups.google.com/group/django-users).


> I assume there's many libraries (SQL Alchemy or others), which could do
> this job. and which migh have less overhead than django.

Ugh.  I've played with SQL Alchemy a few times and every time I've run 
away screaming in the other direction.  I can see how it's useful if you 
need to be totally cross-platform, but, man, if that's what it takes to 
be cross platform, I'm happy being a MySQL bigot all day long.

> 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.

I don't see any reason you couldn't use the Model layer by itself, if 
you want to.  It pretty much stands on its own.

[toc] | [prev] | [next] | [standalone]


#8160

FromNews123 <news1234@free.fr>
Date2011-06-22 03:16 +0200
Message-ID<4E01427E.9000809@free.fr>
In reply to#8155
On 06/22/2011 03:02 AM, Roy Smith wrote:
> In article <4e012e8d$0$23682$426a34cc@news.free.fr>,
>  News123 <news1234@free.fr> wrote:
> 
> 
> I don't see any reason you couldn't use the Model layer by itself, if 
> you want to.  It pretty much stands on its own.

Thanks a lot for confirming,

I have now my small example.

Just wanted to be sure I don't overlook some tiny, but really annoying
detail which would strongly advise against using the model outside of a
web framework.


[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web