check if matching query does not exist return empty string












0















when i try to check if an object exist return it in excel file filed
if not return it an empty 'string'
but i have error in the if sentence



pkg = Package.objects.get(pk=pk)
price_date_all = PackagePricesAndDates.objects.filter(package=pkg)

if price_date_all.get(territory=ter.pk).sales:
list.append(str(price_date_all.get(territory=ter.pk).sales))
else :
list.append('')









share|improve this question

























  • What does the error say?

    – Austin
    Nov 25 '18 at 1:43











  • Please format your code and also add the error stack you got.

    – Chiheb Nexus
    Nov 25 '18 at 1:43











  • @Austin PackagePricesAndDates matching query does not exist. when the program loop to find sales for territory it says that does not exist

    – Abdeljalil Ouzzine
    Nov 25 '18 at 1:45













  • @Austin if does not exit, i want to return an empty string ''

    – Abdeljalil Ouzzine
    Nov 25 '18 at 1:53
















0















when i try to check if an object exist return it in excel file filed
if not return it an empty 'string'
but i have error in the if sentence



pkg = Package.objects.get(pk=pk)
price_date_all = PackagePricesAndDates.objects.filter(package=pkg)

if price_date_all.get(territory=ter.pk).sales:
list.append(str(price_date_all.get(territory=ter.pk).sales))
else :
list.append('')









share|improve this question

























  • What does the error say?

    – Austin
    Nov 25 '18 at 1:43











  • Please format your code and also add the error stack you got.

    – Chiheb Nexus
    Nov 25 '18 at 1:43











  • @Austin PackagePricesAndDates matching query does not exist. when the program loop to find sales for territory it says that does not exist

    – Abdeljalil Ouzzine
    Nov 25 '18 at 1:45













  • @Austin if does not exit, i want to return an empty string ''

    – Abdeljalil Ouzzine
    Nov 25 '18 at 1:53














0












0








0








when i try to check if an object exist return it in excel file filed
if not return it an empty 'string'
but i have error in the if sentence



pkg = Package.objects.get(pk=pk)
price_date_all = PackagePricesAndDates.objects.filter(package=pkg)

if price_date_all.get(territory=ter.pk).sales:
list.append(str(price_date_all.get(territory=ter.pk).sales))
else :
list.append('')









share|improve this question
















when i try to check if an object exist return it in excel file filed
if not return it an empty 'string'
but i have error in the if sentence



pkg = Package.objects.get(pk=pk)
price_date_all = PackagePricesAndDates.objects.filter(package=pkg)

if price_date_all.get(territory=ter.pk).sales:
list.append(str(price_date_all.get(territory=ter.pk).sales))
else :
list.append('')






python django django-queryset






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 25 '18 at 1:50







Abdeljalil Ouzzine

















asked Nov 25 '18 at 1:41









Abdeljalil OuzzineAbdeljalil Ouzzine

105




105













  • What does the error say?

    – Austin
    Nov 25 '18 at 1:43











  • Please format your code and also add the error stack you got.

    – Chiheb Nexus
    Nov 25 '18 at 1:43











  • @Austin PackagePricesAndDates matching query does not exist. when the program loop to find sales for territory it says that does not exist

    – Abdeljalil Ouzzine
    Nov 25 '18 at 1:45













  • @Austin if does not exit, i want to return an empty string ''

    – Abdeljalil Ouzzine
    Nov 25 '18 at 1:53



















  • What does the error say?

    – Austin
    Nov 25 '18 at 1:43











  • Please format your code and also add the error stack you got.

    – Chiheb Nexus
    Nov 25 '18 at 1:43











  • @Austin PackagePricesAndDates matching query does not exist. when the program loop to find sales for territory it says that does not exist

    – Abdeljalil Ouzzine
    Nov 25 '18 at 1:45













  • @Austin if does not exit, i want to return an empty string ''

    – Abdeljalil Ouzzine
    Nov 25 '18 at 1:53

















What does the error say?

– Austin
Nov 25 '18 at 1:43





What does the error say?

– Austin
Nov 25 '18 at 1:43













Please format your code and also add the error stack you got.

– Chiheb Nexus
Nov 25 '18 at 1:43





Please format your code and also add the error stack you got.

– Chiheb Nexus
Nov 25 '18 at 1:43













@Austin PackagePricesAndDates matching query does not exist. when the program loop to find sales for territory it says that does not exist

– Abdeljalil Ouzzine
Nov 25 '18 at 1:45







@Austin PackagePricesAndDates matching query does not exist. when the program loop to find sales for territory it says that does not exist

– Abdeljalil Ouzzine
Nov 25 '18 at 1:45















@Austin if does not exit, i want to return an empty string ''

– Abdeljalil Ouzzine
Nov 25 '18 at 1:53





@Austin if does not exit, i want to return an empty string ''

– Abdeljalil Ouzzine
Nov 25 '18 at 1:53












1 Answer
1






active

oldest

votes


















0














when you use get on a queryset, it will return either the record or raise a DoesNotExist exception.



try:
list.append(str(price_date_all.get(territory=ter.pk).sales))
except DoesNotExist:
list.append('')


FWIW, you should rename the list variable, because its already bound to the list builtin method for creating lists. Don't reuse names that already exist






share|improve this answer
























  • Unresolved reference 'DoesNotExist'

    – Abdeljalil Ouzzine
    Nov 25 '18 at 14:07











  • yes, because you need to import it :-)

    – Jason
    Nov 25 '18 at 14:19











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%2f53463970%2fcheck-if-matching-query-does-not-exist-return-empty-string%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














when you use get on a queryset, it will return either the record or raise a DoesNotExist exception.



try:
list.append(str(price_date_all.get(territory=ter.pk).sales))
except DoesNotExist:
list.append('')


FWIW, you should rename the list variable, because its already bound to the list builtin method for creating lists. Don't reuse names that already exist






share|improve this answer
























  • Unresolved reference 'DoesNotExist'

    – Abdeljalil Ouzzine
    Nov 25 '18 at 14:07











  • yes, because you need to import it :-)

    – Jason
    Nov 25 '18 at 14:19
















0














when you use get on a queryset, it will return either the record or raise a DoesNotExist exception.



try:
list.append(str(price_date_all.get(territory=ter.pk).sales))
except DoesNotExist:
list.append('')


FWIW, you should rename the list variable, because its already bound to the list builtin method for creating lists. Don't reuse names that already exist






share|improve this answer
























  • Unresolved reference 'DoesNotExist'

    – Abdeljalil Ouzzine
    Nov 25 '18 at 14:07











  • yes, because you need to import it :-)

    – Jason
    Nov 25 '18 at 14:19














0












0








0







when you use get on a queryset, it will return either the record or raise a DoesNotExist exception.



try:
list.append(str(price_date_all.get(territory=ter.pk).sales))
except DoesNotExist:
list.append('')


FWIW, you should rename the list variable, because its already bound to the list builtin method for creating lists. Don't reuse names that already exist






share|improve this answer













when you use get on a queryset, it will return either the record or raise a DoesNotExist exception.



try:
list.append(str(price_date_all.get(territory=ter.pk).sales))
except DoesNotExist:
list.append('')


FWIW, you should rename the list variable, because its already bound to the list builtin method for creating lists. Don't reuse names that already exist







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 25 '18 at 1:56









JasonJason

6,2951770150




6,2951770150













  • Unresolved reference 'DoesNotExist'

    – Abdeljalil Ouzzine
    Nov 25 '18 at 14:07











  • yes, because you need to import it :-)

    – Jason
    Nov 25 '18 at 14:19



















  • Unresolved reference 'DoesNotExist'

    – Abdeljalil Ouzzine
    Nov 25 '18 at 14:07











  • yes, because you need to import it :-)

    – Jason
    Nov 25 '18 at 14:19

















Unresolved reference 'DoesNotExist'

– Abdeljalil Ouzzine
Nov 25 '18 at 14:07





Unresolved reference 'DoesNotExist'

– Abdeljalil Ouzzine
Nov 25 '18 at 14:07













yes, because you need to import it :-)

– Jason
Nov 25 '18 at 14:19





yes, because you need to import it :-)

– Jason
Nov 25 '18 at 14:19




















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%2f53463970%2fcheck-if-matching-query-does-not-exist-return-empty-string%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

Wiesbaden

Marschland

Dieringhausen