Annotate Boolean field if item is in a python listHow to fetch the top two products for each product type?django - inlineformset_factory with more than one ForeignKeydjango most efficient way to count same field values in a queryinconsistency in get for multiple databases?Products catalogue: filter by parametersRadio buttons in django adminDjango ImageField overwrites existing path when emptyDjango conditional query by matching boolean valuesHow to set dynamic initial values to django modelform fieldusing sum() to find the aggregated price in a list of items

How to factor a fourth degree polynomial

What's the big deal about the Nazgûl losing their horses?

Why does this function pointer assignment work when assigned directly but not with the conditional operator?

Any way to meet code with 40.7% or 40.44% conduit fill?

Why would "dead languages" be the only languages that spells could be written in?

Multi-user CRUD: Valid, Problem, or Error?

How to reclaim personal item I've lent to the office without burning bridges?

Attach a visible light telescope to the outside of the ISS

Shipped package arrived - didn't order, possible scam?

How do resistors generate different heat if we make the current fixed and changed the voltage and resistance? Notice the flow of charge is constant

Is there an upper limit on the number of cards a character can declare to draw from the Deck of Many Things?

Does the Milky Way orbit around anything?

What happens if the limit of 4 billion files was exceeded in an ext4 partition?

Why do we need a bootloader separate from our application program in microcontrollers?

How important is it for multiple POVs to run chronologically?

What's the difference between a type and a kind?

Was the 45.9°C temperature in France in June 2019 the highest ever recorded in France?

Is there a minimum amount of electricity that can be fed back into the grid?

Do intermediate subdomains need to exist?

Machine Learning Golf: Multiplication

Park the computer

Do grungs have a written language?

Did William Shakespeare hide things in his writings?

SOQL Query (or other means) to get the icon assigned to an object



Annotate Boolean field if item is in a python list


How to fetch the top two products for each product type?django - inlineformset_factory with more than one ForeignKeydjango most efficient way to count same field values in a queryinconsistency in get for multiple databases?Products catalogue: filter by parametersRadio buttons in django adminDjango ImageField overwrites existing path when emptyDjango conditional query by matching boolean valuesHow to set dynamic initial values to django modelform fieldusing sum() to find the aggregated price in a list of items






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








2















I have an app for a Restaurant. And I have this model:



class Product(models.Model):
name = models.CharField(max_length=50)
price = models.PositiveIntegerField()


I have a list called TopSelled like this:



['Beer', 'Burger', ...]


I want to aggregate a boolean field called 'Hot', depending if product item is "top-sell" or not.



So my annotation should be something like this:



Product.objects.all().annotate(if Product.Name in List: HOT = True ELSE Hot = False)


How can I achieve this? Thx!










share|improve this question
























  • Won't it be better to just add a boolean field hot into your Product model? And then when you do Product.objects.all(), you can loop through the QuerySet and just change Product.hot = True.

    – Paolo
    Mar 25 at 20:30











  • I already did what you suggested. But performance is not very good. I am trying to do this with Aggregate(avoid a For Loop) and compare performance. Thanks!

    – Matias Gatti
    Mar 25 at 20:38












  • Oh great, thanks for your input! I didn't think about performance as much.

    – Paolo
    Mar 25 at 20:50

















2















I have an app for a Restaurant. And I have this model:



class Product(models.Model):
name = models.CharField(max_length=50)
price = models.PositiveIntegerField()


I have a list called TopSelled like this:



['Beer', 'Burger', ...]


I want to aggregate a boolean field called 'Hot', depending if product item is "top-sell" or not.



So my annotation should be something like this:



Product.objects.all().annotate(if Product.Name in List: HOT = True ELSE Hot = False)


How can I achieve this? Thx!










share|improve this question
























  • Won't it be better to just add a boolean field hot into your Product model? And then when you do Product.objects.all(), you can loop through the QuerySet and just change Product.hot = True.

    – Paolo
    Mar 25 at 20:30











  • I already did what you suggested. But performance is not very good. I am trying to do this with Aggregate(avoid a For Loop) and compare performance. Thanks!

    – Matias Gatti
    Mar 25 at 20:38












  • Oh great, thanks for your input! I didn't think about performance as much.

    – Paolo
    Mar 25 at 20:50













2












2








2








I have an app for a Restaurant. And I have this model:



class Product(models.Model):
name = models.CharField(max_length=50)
price = models.PositiveIntegerField()


I have a list called TopSelled like this:



['Beer', 'Burger', ...]


I want to aggregate a boolean field called 'Hot', depending if product item is "top-sell" or not.



So my annotation should be something like this:



Product.objects.all().annotate(if Product.Name in List: HOT = True ELSE Hot = False)


How can I achieve this? Thx!










share|improve this question
















I have an app for a Restaurant. And I have this model:



class Product(models.Model):
name = models.CharField(max_length=50)
price = models.PositiveIntegerField()


I have a list called TopSelled like this:



['Beer', 'Burger', ...]


I want to aggregate a boolean field called 'Hot', depending if product item is "top-sell" or not.



So my annotation should be something like this:



Product.objects.all().annotate(if Product.Name in List: HOT = True ELSE Hot = False)


How can I achieve this? Thx!







django django-orm






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 30 at 8:19









Endre Both

3,1711 gold badge13 silver badges22 bronze badges




3,1711 gold badge13 silver badges22 bronze badges










asked Mar 25 at 20:28









Matias GattiMatias Gatti

133 bronze badges




133 bronze badges












  • Won't it be better to just add a boolean field hot into your Product model? And then when you do Product.objects.all(), you can loop through the QuerySet and just change Product.hot = True.

    – Paolo
    Mar 25 at 20:30











  • I already did what you suggested. But performance is not very good. I am trying to do this with Aggregate(avoid a For Loop) and compare performance. Thanks!

    – Matias Gatti
    Mar 25 at 20:38












  • Oh great, thanks for your input! I didn't think about performance as much.

    – Paolo
    Mar 25 at 20:50

















  • Won't it be better to just add a boolean field hot into your Product model? And then when you do Product.objects.all(), you can loop through the QuerySet and just change Product.hot = True.

    – Paolo
    Mar 25 at 20:30











  • I already did what you suggested. But performance is not very good. I am trying to do this with Aggregate(avoid a For Loop) and compare performance. Thanks!

    – Matias Gatti
    Mar 25 at 20:38












  • Oh great, thanks for your input! I didn't think about performance as much.

    – Paolo
    Mar 25 at 20:50
















Won't it be better to just add a boolean field hot into your Product model? And then when you do Product.objects.all(), you can loop through the QuerySet and just change Product.hot = True.

– Paolo
Mar 25 at 20:30





Won't it be better to just add a boolean field hot into your Product model? And then when you do Product.objects.all(), you can loop through the QuerySet and just change Product.hot = True.

– Paolo
Mar 25 at 20:30













I already did what you suggested. But performance is not very good. I am trying to do this with Aggregate(avoid a For Loop) and compare performance. Thanks!

– Matias Gatti
Mar 25 at 20:38






I already did what you suggested. But performance is not very good. I am trying to do this with Aggregate(avoid a For Loop) and compare performance. Thanks!

– Matias Gatti
Mar 25 at 20:38














Oh great, thanks for your input! I didn't think about performance as much.

– Paolo
Mar 25 at 20:50





Oh great, thanks for your input! I didn't think about performance as much.

– Paolo
Mar 25 at 20:50












1 Answer
1






active

oldest

votes


















3














Try annotating with Case:



from django.db.models import BooleanField
from django.db.models.expressions import Case, When

Product.objects.annotate(hot=Case(
When(name__in=hot_list, then=True),
output_field=BooleanField())
).filter(hot=True)





share|improve this answer























  • This was exactly was I was looking for . Thanks !

    – Matias Gatti
    Mar 25 at 21:29










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%2f55345912%2fannotate-boolean-field-if-item-is-in-a-python-list%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









3














Try annotating with Case:



from django.db.models import BooleanField
from django.db.models.expressions import Case, When

Product.objects.annotate(hot=Case(
When(name__in=hot_list, then=True),
output_field=BooleanField())
).filter(hot=True)





share|improve this answer























  • This was exactly was I was looking for . Thanks !

    – Matias Gatti
    Mar 25 at 21:29















3














Try annotating with Case:



from django.db.models import BooleanField
from django.db.models.expressions import Case, When

Product.objects.annotate(hot=Case(
When(name__in=hot_list, then=True),
output_field=BooleanField())
).filter(hot=True)





share|improve this answer























  • This was exactly was I was looking for . Thanks !

    – Matias Gatti
    Mar 25 at 21:29













3












3








3







Try annotating with Case:



from django.db.models import BooleanField
from django.db.models.expressions import Case, When

Product.objects.annotate(hot=Case(
When(name__in=hot_list, then=True),
output_field=BooleanField())
).filter(hot=True)





share|improve this answer













Try annotating with Case:



from django.db.models import BooleanField
from django.db.models.expressions import Case, When

Product.objects.annotate(hot=Case(
When(name__in=hot_list, then=True),
output_field=BooleanField())
).filter(hot=True)






share|improve this answer












share|improve this answer



share|improve this answer










answered Mar 25 at 20:48









Endre BothEndre Both

3,1711 gold badge13 silver badges22 bronze badges




3,1711 gold badge13 silver badges22 bronze badges












  • This was exactly was I was looking for . Thanks !

    – Matias Gatti
    Mar 25 at 21:29

















  • This was exactly was I was looking for . Thanks !

    – Matias Gatti
    Mar 25 at 21:29
















This was exactly was I was looking for . Thanks !

– Matias Gatti
Mar 25 at 21:29





This was exactly was I was looking for . Thanks !

– Matias Gatti
Mar 25 at 21:29








Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.







Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.



















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%2f55345912%2fannotate-boolean-field-if-item-is-in-a-python-list%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

Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript