rozwell @ IRC helped me out substantially by showing me these two links:

http://www.b-list.org/weblog/2007/sep/22/standalone-django-scripts/
http://code.djangoproject.com/wiki/IrcFAQ#Shell

Blaine

On Thu, Jun 25, 2009 at 11:23 AM, Blaine Booher <frik...@gmail.com> wrote:

> Hey everyone,
>   I was wondering the best way to isolate django models so they can be used
> in other python applications.  I have seen the django extensions, which are
> very helpful.  But a 'job' is still coupled to the django environment.
> Ideally I'd like to do this:
>
> * Write some function foo() that uses mysite.myapp.models
> * Be able to import that function into a pure python script.  So I could
> run 'python myscript.py' and it could run foo() without any errors.
>
> I have done the following hack, which lets me import the models, but I get
> a strange error when I try to access the database:
>
> # this is in /project/app/scripts/
> import sys, os
> os.environ['DJANGO_SETTINGS_MODULE'] = 'datamanager.settings'
> sys.path.append('../../../') # Add top level project path (for settings)
> sys.path.append('../../') # Add app level to path
> from myapp.models import mymodel
> # At this point I am good to go, until I try to access the database.
> >>> mymodel.objects
> <django.db.models.manager.Manager object at 0x9284b0c>
> >>> mymodel.objects.all()
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
>   File "/usr/lib/python2.5/site-packages/django/db/models/query.py", line
> 147, in __repr__
>     data = list(self[:REPR_OUTPUT_SIZE + 1])
>   File "/usr/lib/python2.5/site-packages/django/db/models/query.py", line
> 224, in __getitem__
>     qs = self._clone()
>   File "/usr/lib/python2.5/site-packages/django/db/models/query.py", line
> 601, in _clone
>     query = self.query.clone()
>   File "/usr/lib/python2.5/site-packages/django/db/models/sql/query.py",
> line 195, in clone
>     obj.__dict__.update(kwargs)
> TypeError: descriptor '__dict__' for 'Empty' objects doesn't apply to
> 'BaseQuery' object
>
> Does anyone else do this kind of thing?
>
> Blaine
>

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to