On Wed, Oct 27, 2010 at 1:13 AM, Stodge <sto...@gmail.com> wrote: > I have two PostgreSQL (postgresql_psycopg2) databases defined in my > settings; default and scenes. > > If I perform a filter using the 'scenes' DB I get the expected > results: > > Scene.objects.filter(name__contains='ME').using('scenes') > [<Scene: Scene object>, <Scene: Scene object>] > > If I perform a get(), Django seems to get completely confused and > tries to read the data from the default database: > > > Scene.objects.get(id=3).using('scenes') > Traceback (most recent call last): > File "<console>", line 1, in <module> > File "/usr/lib/python2.6/site-packages/django/db/models/manager.py", > line 132, in get > return self.get_query_set().get(*args, **kwargs) > File "/usr/lib/python2.6/site-packages/django/db/models/query.py", > line 341, in get > % self.model._meta.object_name) > DoesNotExist: Scene matching query does not exist. > > Anyone else seen this?
Django isn't getting confused; it's doing exactly what you have asked for. The problem is caused by the fact that get() doesn't return a queryset -- it returns a single object. If, for some reason, there *was* a matching scene in the default database, the next error you would see would be "Scene object has no method 'using()'". You can't apply *any* queryset operation to the end of a get(); get() is a terminal clause on a queryset. get() behaves differently to filter() because filter() returns a queryset, not a single object. As a result, you can apply using() to a filtered queryset to yield a new queryset directed at a different database. Querysets are lazily evaluated, so it doesn't matter when you put the using() call in the query, as long as has been added when you iterate over the final results. Yours, Russ Magee %-) -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@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.