#25811: Error querying models in different databases in one queryset
-------------------------------------+-------------------------------------
Reporter: ebar0n | Owner: nobody
Type: | Status: new
Cleanup/optimization |
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: database | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by jarshwah):
Thanks Shai for clearing that up. Your suggestion about implementing a
custom IN lookup is a good one. That is definitely a way users such as
ebar0n can opt in to new behaviour. The only issue is that they'd need to
carry around and share the code outside of django. It would also be an
all-or-nothing proposition. One `__in` lookup can be registered per field
type, so it's not really something you'd want provided by third party
libs.
I still think a nicer error message would be good though, so I won't
"wontfix" the ticket. But I don't think we should accept the change that
ebar0n is putting forward.
ebar0n, you can use the code you've written in your own application by
doing the following:
{{{
from django.db.models.lookups import In
class MultiDBIn(In):
def process_rhs(self, compiler, connection):
db_rhs = getattr(self.rhs, 'db', None)
# if the argument for the {{__in}} is a subquery, check if the db
is different from the main query,
# and if they are true evaluate the subquery because Django does
not provide support for multiple
# base relations of data
if (db_rhs and db_rhs != connection.alias) or
self.rhs_is_direct_value():
# rhs should be an iterable, we use batch_process_rhs
# to prepare/transform those values
rhs = list(self.rhs)
if not rhs:
from django.db.models.sql.datastructures import
EmptyResultSet
raise EmptyResultSet
sqls, sqls_params = self.batch_process_rhs(compiler,
connection, rhs)
placeholder = '(' + ', '.join(sqls) + ')'
return (placeholder, sqls_params)
else:
return super(In, self).process_rhs(compiler, connection)
IntegerField.register_lookup(MultiDBIn)
CharField.register_lookup(MultiDBIn)
# .. and any other fields you'd want this behaviour to apply to
}}}
Thank you ebar0n for working on this and finding a solution, but I'm
afraid it's not one that we should accept.
--
Ticket URL: <https://code.djangoproject.com/ticket/25811#comment:13>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/064.4a7ace84ca332c3e4dfd96bd8e9f8df0%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.