Django 1.8 QuerySet: .extra syntax in Q object
I have several complex SQL Statements which are used to filter a django Queryset using .extra().
qs.extra(where=["my_jsonb_field-->'my_key'::text LIKE %s"], params=["myValue"])
Since these are queries on json fields, there is no possibility to get rid of these statements (at least not with django 1.8). Is it possible to use Q() objects with the .extra() syntax?
# Q has no .extra
Q().extra(...)
# Q has no .raw
Q().raw(...)
django django-queryset django-1.8 django-q
|
show 1 more comment
I have several complex SQL Statements which are used to filter a django Queryset using .extra().
qs.extra(where=["my_jsonb_field-->'my_key'::text LIKE %s"], params=["myValue"])
Since these are queries on json fields, there is no possibility to get rid of these statements (at least not with django 1.8). Is it possible to use Q() objects with the .extra() syntax?
# Q has no .extra
Q().extra(...)
# Q has no .raw
Q().raw(...)
django django-queryset django-1.8 django-q
extra()
andraw()
work onQuerySet
objects, not onQ
objects. Just make aQuerySet
first, (e.g.qs = MyModel.objects.filter(q)
whereq
is yourQ()
object) then it'll work.
– dirkgroten
Nov 20 '18 at 15:15
I want to use Q objects (or something similar) to run the statement as shown above, not do the query and then additional filtering. As I've stated, there are several of them - sometimes I'd need to chain them together using AND, sometimes using OR, which would be the perfect use case for Q objects IF they'd have .extra(), hence the question.
– Mohl
Nov 20 '18 at 15:29
It’s not possible. But that shouldn’t be a problem: creating aQuerySet
doesn’t “run the statement” (it’ll run only when you actually render the result or iterate through the elements) and sinceQuerySet
s are sets, you can chain them to achieve OR or AND behavior using theunion()
,intersection()
anddifference()
methods.
– dirkgroten
Nov 20 '18 at 15:52
1
Still, you nudged me into the right direction - I could get the raw SQL usingqs.query
, concatenate the strings manually withraw_queries.join(' UNION ')
(orINTERSECT
orEXCEPT
) and putting the resulting string into a new QuerySet usingqs.raw()
! Thanks!
– Mohl
Nov 21 '18 at 9:10
1
Time to move to Django 1.11 :-) Note that Django 1.8 isn't supported anymore since april so there are security issues that haven't been fixed.
– dirkgroten
Nov 21 '18 at 9:26
|
show 1 more comment
I have several complex SQL Statements which are used to filter a django Queryset using .extra().
qs.extra(where=["my_jsonb_field-->'my_key'::text LIKE %s"], params=["myValue"])
Since these are queries on json fields, there is no possibility to get rid of these statements (at least not with django 1.8). Is it possible to use Q() objects with the .extra() syntax?
# Q has no .extra
Q().extra(...)
# Q has no .raw
Q().raw(...)
django django-queryset django-1.8 django-q
I have several complex SQL Statements which are used to filter a django Queryset using .extra().
qs.extra(where=["my_jsonb_field-->'my_key'::text LIKE %s"], params=["myValue"])
Since these are queries on json fields, there is no possibility to get rid of these statements (at least not with django 1.8). Is it possible to use Q() objects with the .extra() syntax?
# Q has no .extra
Q().extra(...)
# Q has no .raw
Q().raw(...)
django django-queryset django-1.8 django-q
django django-queryset django-1.8 django-q
asked Nov 20 '18 at 14:55
MohlMohl
908
908
extra()
andraw()
work onQuerySet
objects, not onQ
objects. Just make aQuerySet
first, (e.g.qs = MyModel.objects.filter(q)
whereq
is yourQ()
object) then it'll work.
– dirkgroten
Nov 20 '18 at 15:15
I want to use Q objects (or something similar) to run the statement as shown above, not do the query and then additional filtering. As I've stated, there are several of them - sometimes I'd need to chain them together using AND, sometimes using OR, which would be the perfect use case for Q objects IF they'd have .extra(), hence the question.
– Mohl
Nov 20 '18 at 15:29
It’s not possible. But that shouldn’t be a problem: creating aQuerySet
doesn’t “run the statement” (it’ll run only when you actually render the result or iterate through the elements) and sinceQuerySet
s are sets, you can chain them to achieve OR or AND behavior using theunion()
,intersection()
anddifference()
methods.
– dirkgroten
Nov 20 '18 at 15:52
1
Still, you nudged me into the right direction - I could get the raw SQL usingqs.query
, concatenate the strings manually withraw_queries.join(' UNION ')
(orINTERSECT
orEXCEPT
) and putting the resulting string into a new QuerySet usingqs.raw()
! Thanks!
– Mohl
Nov 21 '18 at 9:10
1
Time to move to Django 1.11 :-) Note that Django 1.8 isn't supported anymore since april so there are security issues that haven't been fixed.
– dirkgroten
Nov 21 '18 at 9:26
|
show 1 more comment
extra()
andraw()
work onQuerySet
objects, not onQ
objects. Just make aQuerySet
first, (e.g.qs = MyModel.objects.filter(q)
whereq
is yourQ()
object) then it'll work.
– dirkgroten
Nov 20 '18 at 15:15
I want to use Q objects (or something similar) to run the statement as shown above, not do the query and then additional filtering. As I've stated, there are several of them - sometimes I'd need to chain them together using AND, sometimes using OR, which would be the perfect use case for Q objects IF they'd have .extra(), hence the question.
– Mohl
Nov 20 '18 at 15:29
It’s not possible. But that shouldn’t be a problem: creating aQuerySet
doesn’t “run the statement” (it’ll run only when you actually render the result or iterate through the elements) and sinceQuerySet
s are sets, you can chain them to achieve OR or AND behavior using theunion()
,intersection()
anddifference()
methods.
– dirkgroten
Nov 20 '18 at 15:52
1
Still, you nudged me into the right direction - I could get the raw SQL usingqs.query
, concatenate the strings manually withraw_queries.join(' UNION ')
(orINTERSECT
orEXCEPT
) and putting the resulting string into a new QuerySet usingqs.raw()
! Thanks!
– Mohl
Nov 21 '18 at 9:10
1
Time to move to Django 1.11 :-) Note that Django 1.8 isn't supported anymore since april so there are security issues that haven't been fixed.
– dirkgroten
Nov 21 '18 at 9:26
extra()
and raw()
work on QuerySet
objects, not on Q
objects. Just make a QuerySet
first, (e.g. qs = MyModel.objects.filter(q)
where q
is your Q()
object) then it'll work.– dirkgroten
Nov 20 '18 at 15:15
extra()
and raw()
work on QuerySet
objects, not on Q
objects. Just make a QuerySet
first, (e.g. qs = MyModel.objects.filter(q)
where q
is your Q()
object) then it'll work.– dirkgroten
Nov 20 '18 at 15:15
I want to use Q objects (or something similar) to run the statement as shown above, not do the query and then additional filtering. As I've stated, there are several of them - sometimes I'd need to chain them together using AND, sometimes using OR, which would be the perfect use case for Q objects IF they'd have .extra(), hence the question.
– Mohl
Nov 20 '18 at 15:29
I want to use Q objects (or something similar) to run the statement as shown above, not do the query and then additional filtering. As I've stated, there are several of them - sometimes I'd need to chain them together using AND, sometimes using OR, which would be the perfect use case for Q objects IF they'd have .extra(), hence the question.
– Mohl
Nov 20 '18 at 15:29
It’s not possible. But that shouldn’t be a problem: creating a
QuerySet
doesn’t “run the statement” (it’ll run only when you actually render the result or iterate through the elements) and since QuerySet
s are sets, you can chain them to achieve OR or AND behavior using the union()
, intersection()
and difference()
methods.– dirkgroten
Nov 20 '18 at 15:52
It’s not possible. But that shouldn’t be a problem: creating a
QuerySet
doesn’t “run the statement” (it’ll run only when you actually render the result or iterate through the elements) and since QuerySet
s are sets, you can chain them to achieve OR or AND behavior using the union()
, intersection()
and difference()
methods.– dirkgroten
Nov 20 '18 at 15:52
1
1
Still, you nudged me into the right direction - I could get the raw SQL using
qs.query
, concatenate the strings manually with raw_queries.join(' UNION ')
(or INTERSECT
or EXCEPT
) and putting the resulting string into a new QuerySet using qs.raw()
! Thanks!– Mohl
Nov 21 '18 at 9:10
Still, you nudged me into the right direction - I could get the raw SQL using
qs.query
, concatenate the strings manually with raw_queries.join(' UNION ')
(or INTERSECT
or EXCEPT
) and putting the resulting string into a new QuerySet using qs.raw()
! Thanks!– Mohl
Nov 21 '18 at 9:10
1
1
Time to move to Django 1.11 :-) Note that Django 1.8 isn't supported anymore since april so there are security issues that haven't been fixed.
– dirkgroten
Nov 21 '18 at 9:26
Time to move to Django 1.11 :-) Note that Django 1.8 isn't supported anymore since april so there are security issues that haven't been fixed.
– dirkgroten
Nov 21 '18 at 9:26
|
show 1 more comment
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53395702%2fdjango-1-8-queryset-extra-syntax-in-q-object%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53395702%2fdjango-1-8-queryset-extra-syntax-in-q-object%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
extra()
andraw()
work onQuerySet
objects, not onQ
objects. Just make aQuerySet
first, (e.g.qs = MyModel.objects.filter(q)
whereq
is yourQ()
object) then it'll work.– dirkgroten
Nov 20 '18 at 15:15
I want to use Q objects (or something similar) to run the statement as shown above, not do the query and then additional filtering. As I've stated, there are several of them - sometimes I'd need to chain them together using AND, sometimes using OR, which would be the perfect use case for Q objects IF they'd have .extra(), hence the question.
– Mohl
Nov 20 '18 at 15:29
It’s not possible. But that shouldn’t be a problem: creating a
QuerySet
doesn’t “run the statement” (it’ll run only when you actually render the result or iterate through the elements) and sinceQuerySet
s are sets, you can chain them to achieve OR or AND behavior using theunion()
,intersection()
anddifference()
methods.– dirkgroten
Nov 20 '18 at 15:52
1
Still, you nudged me into the right direction - I could get the raw SQL using
qs.query
, concatenate the strings manually withraw_queries.join(' UNION ')
(orINTERSECT
orEXCEPT
) and putting the resulting string into a new QuerySet usingqs.raw()
! Thanks!– Mohl
Nov 21 '18 at 9:10
1
Time to move to Django 1.11 :-) Note that Django 1.8 isn't supported anymore since april so there are security issues that haven't been fixed.
– dirkgroten
Nov 21 '18 at 9:26