Why is Django Paramaterized query not working












0















I have been struggling with this simple query in django. I have checked a lot over internet. I tried using similar syntax - yet no luck.



In my application on html page I need to display some specific record. And for that i want to use parameterized select statement. But the query is not working..



I have checked id2 is getting correct value from previous html page..



And I know I can use APIs but for my databases classes I need to use raw queries in my application.



Can someone please help me here...



def details(request, id2):

temp = 'test3'
data = Posts.objects.raw('''select * from posts_posts where posts_posts.id = %s ''', id2)
context ={
'post' : data,









share|improve this question

























  • Hi, it's not clear for me what's your issue actually.

    – Linovia
    Nov 21 '18 at 21:12











  • Hi..I am trying to fecth data from postgresql and print that on html page.However the query is not working correctly. No data is getting selected. The simple select query is working fine but this parameterized query is not working correctly.

    – sukhada sheth
    Nov 21 '18 at 23:42













  • This is completely pointless. There's no need for a raw query here. You should do Post.objects.filter(id=2).

    – Daniel Roseman
    Nov 22 '18 at 9:34













  • yes i agree... as i already mentioned in my original post...this assignmnet is for my database management class...i cannot use direct APIs... I am supposed to use raw queries...

    – sukhada sheth
    Nov 22 '18 at 15:43
















0















I have been struggling with this simple query in django. I have checked a lot over internet. I tried using similar syntax - yet no luck.



In my application on html page I need to display some specific record. And for that i want to use parameterized select statement. But the query is not working..



I have checked id2 is getting correct value from previous html page..



And I know I can use APIs but for my databases classes I need to use raw queries in my application.



Can someone please help me here...



def details(request, id2):

temp = 'test3'
data = Posts.objects.raw('''select * from posts_posts where posts_posts.id = %s ''', id2)
context ={
'post' : data,









share|improve this question

























  • Hi, it's not clear for me what's your issue actually.

    – Linovia
    Nov 21 '18 at 21:12











  • Hi..I am trying to fecth data from postgresql and print that on html page.However the query is not working correctly. No data is getting selected. The simple select query is working fine but this parameterized query is not working correctly.

    – sukhada sheth
    Nov 21 '18 at 23:42













  • This is completely pointless. There's no need for a raw query here. You should do Post.objects.filter(id=2).

    – Daniel Roseman
    Nov 22 '18 at 9:34













  • yes i agree... as i already mentioned in my original post...this assignmnet is for my database management class...i cannot use direct APIs... I am supposed to use raw queries...

    – sukhada sheth
    Nov 22 '18 at 15:43














0












0








0


1






I have been struggling with this simple query in django. I have checked a lot over internet. I tried using similar syntax - yet no luck.



In my application on html page I need to display some specific record. And for that i want to use parameterized select statement. But the query is not working..



I have checked id2 is getting correct value from previous html page..



And I know I can use APIs but for my databases classes I need to use raw queries in my application.



Can someone please help me here...



def details(request, id2):

temp = 'test3'
data = Posts.objects.raw('''select * from posts_posts where posts_posts.id = %s ''', id2)
context ={
'post' : data,









share|improve this question
















I have been struggling with this simple query in django. I have checked a lot over internet. I tried using similar syntax - yet no luck.



In my application on html page I need to display some specific record. And for that i want to use parameterized select statement. But the query is not working..



I have checked id2 is getting correct value from previous html page..



And I know I can use APIs but for my databases classes I need to use raw queries in my application.



Can someone please help me here...



def details(request, id2):

temp = 'test3'
data = Posts.objects.raw('''select * from posts_posts where posts_posts.id = %s ''', id2)
context ={
'post' : data,






django django-models






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 '18 at 8:17









Andrew Backer

4,76623459




4,76623459










asked Nov 21 '18 at 19:17









sukhada shethsukhada sheth

81




81













  • Hi, it's not clear for me what's your issue actually.

    – Linovia
    Nov 21 '18 at 21:12











  • Hi..I am trying to fecth data from postgresql and print that on html page.However the query is not working correctly. No data is getting selected. The simple select query is working fine but this parameterized query is not working correctly.

    – sukhada sheth
    Nov 21 '18 at 23:42













  • This is completely pointless. There's no need for a raw query here. You should do Post.objects.filter(id=2).

    – Daniel Roseman
    Nov 22 '18 at 9:34













  • yes i agree... as i already mentioned in my original post...this assignmnet is for my database management class...i cannot use direct APIs... I am supposed to use raw queries...

    – sukhada sheth
    Nov 22 '18 at 15:43



















  • Hi, it's not clear for me what's your issue actually.

    – Linovia
    Nov 21 '18 at 21:12











  • Hi..I am trying to fecth data from postgresql and print that on html page.However the query is not working correctly. No data is getting selected. The simple select query is working fine but this parameterized query is not working correctly.

    – sukhada sheth
    Nov 21 '18 at 23:42













  • This is completely pointless. There's no need for a raw query here. You should do Post.objects.filter(id=2).

    – Daniel Roseman
    Nov 22 '18 at 9:34













  • yes i agree... as i already mentioned in my original post...this assignmnet is for my database management class...i cannot use direct APIs... I am supposed to use raw queries...

    – sukhada sheth
    Nov 22 '18 at 15:43

















Hi, it's not clear for me what's your issue actually.

– Linovia
Nov 21 '18 at 21:12





Hi, it's not clear for me what's your issue actually.

– Linovia
Nov 21 '18 at 21:12













Hi..I am trying to fecth data from postgresql and print that on html page.However the query is not working correctly. No data is getting selected. The simple select query is working fine but this parameterized query is not working correctly.

– sukhada sheth
Nov 21 '18 at 23:42







Hi..I am trying to fecth data from postgresql and print that on html page.However the query is not working correctly. No data is getting selected. The simple select query is working fine but this parameterized query is not working correctly.

– sukhada sheth
Nov 21 '18 at 23:42















This is completely pointless. There's no need for a raw query here. You should do Post.objects.filter(id=2).

– Daniel Roseman
Nov 22 '18 at 9:34







This is completely pointless. There's no need for a raw query here. You should do Post.objects.filter(id=2).

– Daniel Roseman
Nov 22 '18 at 9:34















yes i agree... as i already mentioned in my original post...this assignmnet is for my database management class...i cannot use direct APIs... I am supposed to use raw queries...

– sukhada sheth
Nov 22 '18 at 15:43





yes i agree... as i already mentioned in my original post...this assignmnet is for my database management class...i cannot use direct APIs... I am supposed to use raw queries...

– sukhada sheth
Nov 22 '18 at 15:43












1 Answer
1






active

oldest

votes


















0














If you run that code you will see an error, since that is not the correct format for a call to raw. If you can't see the error output anywhere, then you have yet another problem for another post.



The correct format for raw is: .raw(sql, iterable-values), like this:



posts = Posts.objects.raw("select * ..", [id2, ])  # or use (id2,) for a tuple
<RawQuerySet: select * from ... where id = 5>


To get the actual list, since that just gives you a Query, you need to evaluate it somehow:



posts_list = list(posts)
first_post = posts[0]


Be careful, if you don't evaluate the QuerySet then it can be re-run a second time. Please convert it to a list() before doing further operations on it.






share|improve this answer
























  • thanks a ton...your suggestion to evaluate the query helped. :)

    – sukhada sheth
    Dec 3 '18 at 21:00











  • Welcome to Stack Overflow! Please remember to upvote questions you find helpful, and make any responses as "answered" if they solve your issue.

    – Andrew Backer
    Dec 4 '18 at 3:37











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%2f53419115%2fwhy-is-django-paramaterized-query-not-working%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














If you run that code you will see an error, since that is not the correct format for a call to raw. If you can't see the error output anywhere, then you have yet another problem for another post.



The correct format for raw is: .raw(sql, iterable-values), like this:



posts = Posts.objects.raw("select * ..", [id2, ])  # or use (id2,) for a tuple
<RawQuerySet: select * from ... where id = 5>


To get the actual list, since that just gives you a Query, you need to evaluate it somehow:



posts_list = list(posts)
first_post = posts[0]


Be careful, if you don't evaluate the QuerySet then it can be re-run a second time. Please convert it to a list() before doing further operations on it.






share|improve this answer
























  • thanks a ton...your suggestion to evaluate the query helped. :)

    – sukhada sheth
    Dec 3 '18 at 21:00











  • Welcome to Stack Overflow! Please remember to upvote questions you find helpful, and make any responses as "answered" if they solve your issue.

    – Andrew Backer
    Dec 4 '18 at 3:37
















0














If you run that code you will see an error, since that is not the correct format for a call to raw. If you can't see the error output anywhere, then you have yet another problem for another post.



The correct format for raw is: .raw(sql, iterable-values), like this:



posts = Posts.objects.raw("select * ..", [id2, ])  # or use (id2,) for a tuple
<RawQuerySet: select * from ... where id = 5>


To get the actual list, since that just gives you a Query, you need to evaluate it somehow:



posts_list = list(posts)
first_post = posts[0]


Be careful, if you don't evaluate the QuerySet then it can be re-run a second time. Please convert it to a list() before doing further operations on it.






share|improve this answer
























  • thanks a ton...your suggestion to evaluate the query helped. :)

    – sukhada sheth
    Dec 3 '18 at 21:00











  • Welcome to Stack Overflow! Please remember to upvote questions you find helpful, and make any responses as "answered" if they solve your issue.

    – Andrew Backer
    Dec 4 '18 at 3:37














0












0








0







If you run that code you will see an error, since that is not the correct format for a call to raw. If you can't see the error output anywhere, then you have yet another problem for another post.



The correct format for raw is: .raw(sql, iterable-values), like this:



posts = Posts.objects.raw("select * ..", [id2, ])  # or use (id2,) for a tuple
<RawQuerySet: select * from ... where id = 5>


To get the actual list, since that just gives you a Query, you need to evaluate it somehow:



posts_list = list(posts)
first_post = posts[0]


Be careful, if you don't evaluate the QuerySet then it can be re-run a second time. Please convert it to a list() before doing further operations on it.






share|improve this answer













If you run that code you will see an error, since that is not the correct format for a call to raw. If you can't see the error output anywhere, then you have yet another problem for another post.



The correct format for raw is: .raw(sql, iterable-values), like this:



posts = Posts.objects.raw("select * ..", [id2, ])  # or use (id2,) for a tuple
<RawQuerySet: select * from ... where id = 5>


To get the actual list, since that just gives you a Query, you need to evaluate it somehow:



posts_list = list(posts)
first_post = posts[0]


Be careful, if you don't evaluate the QuerySet then it can be re-run a second time. Please convert it to a list() before doing further operations on it.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 22 '18 at 8:14









Andrew BackerAndrew Backer

4,76623459




4,76623459













  • thanks a ton...your suggestion to evaluate the query helped. :)

    – sukhada sheth
    Dec 3 '18 at 21:00











  • Welcome to Stack Overflow! Please remember to upvote questions you find helpful, and make any responses as "answered" if they solve your issue.

    – Andrew Backer
    Dec 4 '18 at 3:37



















  • thanks a ton...your suggestion to evaluate the query helped. :)

    – sukhada sheth
    Dec 3 '18 at 21:00











  • Welcome to Stack Overflow! Please remember to upvote questions you find helpful, and make any responses as "answered" if they solve your issue.

    – Andrew Backer
    Dec 4 '18 at 3:37

















thanks a ton...your suggestion to evaluate the query helped. :)

– sukhada sheth
Dec 3 '18 at 21:00





thanks a ton...your suggestion to evaluate the query helped. :)

– sukhada sheth
Dec 3 '18 at 21:00













Welcome to Stack Overflow! Please remember to upvote questions you find helpful, and make any responses as "answered" if they solve your issue.

– Andrew Backer
Dec 4 '18 at 3:37





Welcome to Stack Overflow! Please remember to upvote questions you find helpful, and make any responses as "answered" if they solve your issue.

– Andrew Backer
Dec 4 '18 at 3:37




















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%2f53419115%2fwhy-is-django-paramaterized-query-not-working%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

Paul Cézanne

UIScrollView CustomStickyHeader Resize height generates problems when scroll is too fast

Angular material date-picker (MatDatepicker) auto completes the date on focus out