-
Notifications
You must be signed in to change notification settings - Fork 53
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for Subquery expressions #182
Comments
@stefan6419846 Do you think you could take a look here and help me figure out how to best support annotating using a |
Certainly not today, but I might have a look at it in a few days. Nevertheless, I have to admit that I have mostly worked on fixing compatibility with more recent Django versions (to allow usage in my own code) and reviewing PRs, while not having a complete overview over the whole module. |
That's fair enough. Is there anyone else besides you that might be able to help? Could you at least tell me what |
If in doubt, I would point to @stphivos. |
Gotcha. I'll wait for @stphivos to circle back, then. Thank you! |
I'm afraid this one is a bit tricky. Maybe a possible solution would be to somehow use a nested |
I've tried tapping into the In practice, something like this should be feasible: def get_attribute(obj, attr, default=None):
# ...
if isinstance(attr, Subquery):
subquery_query = attr.query
subquery_model = subquery_query.model
subquery_where = subquery_query.where
for child in subquery_where.children:
if hasattr(child, 'lhs') and hasattr(child, 'rhs'):
field_name = child.lhs.target.name
outer_ref_value = getattr(obj, child.rhs.name)
subquery_result = [item for item in subquery_model.objects.all() if getattr(item, field_name) == outer_ref_value]
return subquery_result[0] if subquery_result else None, None
return None, None This is not trying to be a comprehensive (or even working) solution. It's only aimed at tackling the query example from my OG comment: Subquery(ms.filter(value=OuterRef("value")) The first of many problems is that, currently, passing a A kind of nested Since self.query = getattr(queryset, "query", queryset).clone() Having a @property
def query(self):
return self._mockset_class()(*self.items, clone=self) Would make the |
@stphivos Any updates here? Would be really nice to have this supported. |
Related to #181.
After introducing changes from #181 to properly mock
MockSet.query
, the following breaks becauseutils.get_attribute
doesn't currently handleSubquery
expressions.I've been playing around the codebase, trying to see if I could fit this in, but hit a wall. In contrast to all other cases (
F
,Case
,Value
andCoalesce
), aSubquery
cannot be immediately resolved so I'm not too sure how the logic should go.Happy to help and contribute if you point me in the right direction.
The text was updated successfully, but these errors were encountered: