On Mon, 2006-07-10 at 22:24 -0500, Jacob Kaplan-Moss wrote:
> On Jul 10, 2006, at 10:09 PM, jason pellerin wrote:
[...]
> >> Finally: will there be syntax to use a specific connection for some
> >> operations even if that model's set up to use a different backend?
> >> I'd hope so, but I'm not sure I can come up with a good syntax...
> >
> > Well, you can always set Model._meta.db_connection = 'foo' at any
> > time. Or create a manager that pulls the connection to use from
> > someplace other than self.model._meta.connection, though that would be
> > difficult without some additional abstraction. I'm not quite sure what
> > the use of this would be, but that may mean there's a need that I just
> > can't see.
> 
> I posted a couple of example use-cases in response to Malcolm.   
> Manually setting ``db_connection`` is ugly but workable; I'll think  
> some more about a more elegant way of doing it.

One possibility based on what you (Jacob) suggested earlier: if the db
connection is a property of the model manager, then it is something you
can override by using a different manager. We already have
get_query_set() as the "default query set" for a manager. We could have
get_db_connection() as the way to get the db connection (or make it a
property or an attribute or whatever; the concept is the thing here).
Then you can use various managers to get data from different databases.

True, you could end having code that looks like this:

        if preferred_connection = 'first_db':
           manager = 'first'
        else:
           manager = 'second'
        data = getattr(MyModel, manager).filter(...)
        
which could get old after the first two or three times, although
wrapping that in a convenience function
(MyModel.get_manager(manager).filter(...)) is not crazy in those cases.

In passing, storing the *default* db connection in the Option class
doesn't feel too ugly to me: it's a place to store model-specific (as
opposed to instance-specific) information. We just happen to populate it
mostly from Meta (I'm rewriting history saying it this way, I realise,
but does that sound like a reasoning that makes sense).

Malcolm


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django developers" group.
To post to this group, send email to django-developers@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-developers
-~----------~----~----~----~------~----~------~--~---

Reply via email to