Django 1.8 QuerySet: .extra syntax in Q object












0















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(...)









share|improve this question























  • 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











  • 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 QuerySets 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





    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





    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


















0















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(...)









share|improve this question























  • 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











  • 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 QuerySets 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





    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





    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
















0












0








0


1






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(...)









share|improve this question














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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 20 '18 at 14:55









MohlMohl

908




908













  • 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











  • 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 QuerySets 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





    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





    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











  • 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 QuerySets 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





    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





    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 QuerySets 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 QuerySets 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














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
});


}
});














draft saved

draft discarded


















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
















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

How to change which sound is reproduced for terminal bell?

Can I use Tabulator js library in my java Spring + Thymeleaf project?

Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents