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;
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
add a comment |
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
Won't it be better to just add a boolean field hot into your Product model? And then when you doProduct.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
add a comment |
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
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
django django-orm
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 doProduct.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
add a comment |
Won't it be better to just add a boolean field hot into your Product model? And then when you doProduct.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
add a comment |
1 Answer
1
active
oldest
votes
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)
This was exactly was I was looking for . Thanks !
– Matias Gatti
Mar 25 at 21:29
add a comment |
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%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
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)
This was exactly was I was looking for . Thanks !
– Matias Gatti
Mar 25 at 21:29
add a comment |
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)
This was exactly was I was looking for . Thanks !
– Matias Gatti
Mar 25 at 21:29
add a comment |
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)
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)
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
add a comment |
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
add a comment |
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.
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%2f55345912%2fannotate-boolean-field-if-item-is-in-a-python-list%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
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