How to include count and sum query sets for models & viewsHow to query as GROUP BY in django?How to view corresponding SQL query of the Django ORM's queryset?Use Django Model class inheritance to create an audit log for a tableAn issue filtering related models inside the model definitionCalculating the sum of aggregate query setdjango model query setWhat is wrong with my models.py?Django: New class added in model.py not showing in admin siteCreate a new model which have all fields of currently existing modelDirect assignment to the forward side of a many-to-many set is prohibited. Use particular.set() instead
In what sense are the equations of motion conserved by symmetries?
Looking for a new job because of relocation - is it okay to tell the real reason?
How do I get the =LEFT function in excel, to also take the number zero as the first number?
Can ads on a page read my password?
Best way to explain to my boss that I cannot attend a team summit because it is on Rosh Hashana or any other Jewish Holiday
Why do implementations of "stdint.h" disagree on the definition of UINT8_C?
Japanese equivalent of a brain fart
What are the examples (applications) of the MIPs in which the objective function has nonzero coefficients for only continuous variables?
Is it really ~648.69 km/s Delta-V to "Land" on the Surface of the Sun?
Did silent film actors actually say their lines or did they simply improvise “dialogue” while being filmed?
Need help understanding lens reach
Do other countries guarantee freedoms that the United States does not have?
Casting Goblin Matron with Plague Engineer on the battlefield
Traveling from Germany to other countries by train?
How quickly could a country build a tall concrete wall around a city?
Did Apollo leave poop on the moon?
"How do you solve a problem like Maria?"
Does this put me at risk for identity theft?
Why should public servants be apolitical?
Why couldn't soldiers sight their own weapons without officers' orders?
Where to pee in London?
What are good ways to improve as a writer other than writing courses?
Secure my password from unsafe servers
Look mom! I made my own (Base 10) numeral system!
How to include count and sum query sets for models & views
How to query as GROUP BY in django?How to view corresponding SQL query of the Django ORM's queryset?Use Django Model class inheritance to create an audit log for a tableAn issue filtering related models inside the model definitionCalculating the sum of aggregate query setdjango model query setWhat is wrong with my models.py?Django: New class added in model.py not showing in admin siteCreate a new model which have all fields of currently existing modelDirect assignment to the forward side of a many-to-many set is prohibited. Use particular.set() instead
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I am trying to add up the number of tickets (count) and the (sum) of the price of each ticket purchased. The ticket is a fixed price of $25 each. I've used this in my models.py:
class Ticket(models.Model):
venue=models.CharField(max_length=100)
quantity=models.IntegerField(null=True)
price=models.DecimalField(max_digits=10, decimal_places=2)
loop=models.BooleanField(default=True)
purchaser = models.ForeignKey(User, related_name="purchases",
on_delete=models.PROTECT)
created_at=models.DateTimeField(auto_now_add=True)
updated_at=models.DateTimeField(auto_now=True)
I am confused where to do the query sets to get the total number of tickets and the summation of the prices? Would it be
total_price=Ticket.objects.all().aggregate(Sum('price'))
ticket_count = Tickets.objects.count()
Would the above variables (ex: total_price and _ticket_count) be included as columns in the models or it needs to be stated only in the views? Can quantity be the same as count? Thank you very, very much!
django-models django-views
add a comment |
I am trying to add up the number of tickets (count) and the (sum) of the price of each ticket purchased. The ticket is a fixed price of $25 each. I've used this in my models.py:
class Ticket(models.Model):
venue=models.CharField(max_length=100)
quantity=models.IntegerField(null=True)
price=models.DecimalField(max_digits=10, decimal_places=2)
loop=models.BooleanField(default=True)
purchaser = models.ForeignKey(User, related_name="purchases",
on_delete=models.PROTECT)
created_at=models.DateTimeField(auto_now_add=True)
updated_at=models.DateTimeField(auto_now=True)
I am confused where to do the query sets to get the total number of tickets and the summation of the prices? Would it be
total_price=Ticket.objects.all().aggregate(Sum('price'))
ticket_count = Tickets.objects.count()
Would the above variables (ex: total_price and _ticket_count) be included as columns in the models or it needs to be stated only in the views? Can quantity be the same as count? Thank you very, very much!
django-models django-views
I think you're storing number of tickets purchased by a user in quantity ?
– Atul Kumar
Mar 29 at 10:13
Hi Atul, that is correct. I wanted to store the number or in this case the "quantity" of tickets times the fixed price of $25. I also wanted to see if I can add taxes to it. I tried adding a field of taxes as 0.0725. Can you please advise on how to go about querying it? I added: @property def total(self): q= self.price * self.quantity t= q *0.0725 totalPrice= t + self.price return totalPrice under Ticket model but to no avail. Thank you for your response, your feedback would be much appreciated!
– Keiko
Mar 30 at 19:03
add a comment |
I am trying to add up the number of tickets (count) and the (sum) of the price of each ticket purchased. The ticket is a fixed price of $25 each. I've used this in my models.py:
class Ticket(models.Model):
venue=models.CharField(max_length=100)
quantity=models.IntegerField(null=True)
price=models.DecimalField(max_digits=10, decimal_places=2)
loop=models.BooleanField(default=True)
purchaser = models.ForeignKey(User, related_name="purchases",
on_delete=models.PROTECT)
created_at=models.DateTimeField(auto_now_add=True)
updated_at=models.DateTimeField(auto_now=True)
I am confused where to do the query sets to get the total number of tickets and the summation of the prices? Would it be
total_price=Ticket.objects.all().aggregate(Sum('price'))
ticket_count = Tickets.objects.count()
Would the above variables (ex: total_price and _ticket_count) be included as columns in the models or it needs to be stated only in the views? Can quantity be the same as count? Thank you very, very much!
django-models django-views
I am trying to add up the number of tickets (count) and the (sum) of the price of each ticket purchased. The ticket is a fixed price of $25 each. I've used this in my models.py:
class Ticket(models.Model):
venue=models.CharField(max_length=100)
quantity=models.IntegerField(null=True)
price=models.DecimalField(max_digits=10, decimal_places=2)
loop=models.BooleanField(default=True)
purchaser = models.ForeignKey(User, related_name="purchases",
on_delete=models.PROTECT)
created_at=models.DateTimeField(auto_now_add=True)
updated_at=models.DateTimeField(auto_now=True)
I am confused where to do the query sets to get the total number of tickets and the summation of the prices? Would it be
total_price=Ticket.objects.all().aggregate(Sum('price'))
ticket_count = Tickets.objects.count()
Would the above variables (ex: total_price and _ticket_count) be included as columns in the models or it needs to be stated only in the views? Can quantity be the same as count? Thank you very, very much!
django-models django-views
django-models django-views
edited Mar 27 at 6:20
Stefan Becker
4,5913 gold badges11 silver badges25 bronze badges
4,5913 gold badges11 silver badges25 bronze badges
asked Mar 27 at 6:02
KeikoKeiko
246 bronze badges
246 bronze badges
I think you're storing number of tickets purchased by a user in quantity ?
– Atul Kumar
Mar 29 at 10:13
Hi Atul, that is correct. I wanted to store the number or in this case the "quantity" of tickets times the fixed price of $25. I also wanted to see if I can add taxes to it. I tried adding a field of taxes as 0.0725. Can you please advise on how to go about querying it? I added: @property def total(self): q= self.price * self.quantity t= q *0.0725 totalPrice= t + self.price return totalPrice under Ticket model but to no avail. Thank you for your response, your feedback would be much appreciated!
– Keiko
Mar 30 at 19:03
add a comment |
I think you're storing number of tickets purchased by a user in quantity ?
– Atul Kumar
Mar 29 at 10:13
Hi Atul, that is correct. I wanted to store the number or in this case the "quantity" of tickets times the fixed price of $25. I also wanted to see if I can add taxes to it. I tried adding a field of taxes as 0.0725. Can you please advise on how to go about querying it? I added: @property def total(self): q= self.price * self.quantity t= q *0.0725 totalPrice= t + self.price return totalPrice under Ticket model but to no avail. Thank you for your response, your feedback would be much appreciated!
– Keiko
Mar 30 at 19:03
I think you're storing number of tickets purchased by a user in quantity ?
– Atul Kumar
Mar 29 at 10:13
I think you're storing number of tickets purchased by a user in quantity ?
– Atul Kumar
Mar 29 at 10:13
Hi Atul, that is correct. I wanted to store the number or in this case the "quantity" of tickets times the fixed price of $25. I also wanted to see if I can add taxes to it. I tried adding a field of taxes as 0.0725. Can you please advise on how to go about querying it? I added: @property def total(self): q= self.price * self.quantity t= q *0.0725 totalPrice= t + self.price return totalPrice under Ticket model but to no avail. Thank you for your response, your feedback would be much appreciated!
– Keiko
Mar 30 at 19:03
Hi Atul, that is correct. I wanted to store the number or in this case the "quantity" of tickets times the fixed price of $25. I also wanted to see if I can add taxes to it. I tried adding a field of taxes as 0.0725. Can you please advise on how to go about querying it? I added: @property def total(self): q= self.price * self.quantity t= q *0.0725 totalPrice= t + self.price return totalPrice under Ticket model but to no avail. Thank you for your response, your feedback would be much appreciated!
– Keiko
Mar 30 at 19:03
add a comment |
1 Answer
1
active
oldest
votes
More on aggregate
from django.db.models import Sum
# This will give total tickets sold to all the user
total_tickets = Ticket.objects.aggregate(Sum('quantity'))
total_cost = total_tickets * 25
Thank you Atul for your response. Is that supposed to be included right below the Ticket model or views? I tried to do that and it didn't go through. I tried this query: "total":Ticket.objects.filter(purchaser=User.objects.get(id=request.session['user_id'])).aggregate(total_cost=Sum(F('price')*F('quantity'), output_field=FloatField()) in views.py but it's invalid synthax. FYI, I included--default=25.00 in the price field--would these be correct? Thank you SO MUCH!
– Keiko
Apr 3 at 2:06
In views, you need to do this.
– Atul Kumar
Apr 3 at 3:38
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%2f55370737%2fhow-to-include-count-and-sum-query-sets-for-models-views%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
More on aggregate
from django.db.models import Sum
# This will give total tickets sold to all the user
total_tickets = Ticket.objects.aggregate(Sum('quantity'))
total_cost = total_tickets * 25
Thank you Atul for your response. Is that supposed to be included right below the Ticket model or views? I tried to do that and it didn't go through. I tried this query: "total":Ticket.objects.filter(purchaser=User.objects.get(id=request.session['user_id'])).aggregate(total_cost=Sum(F('price')*F('quantity'), output_field=FloatField()) in views.py but it's invalid synthax. FYI, I included--default=25.00 in the price field--would these be correct? Thank you SO MUCH!
– Keiko
Apr 3 at 2:06
In views, you need to do this.
– Atul Kumar
Apr 3 at 3:38
add a comment |
More on aggregate
from django.db.models import Sum
# This will give total tickets sold to all the user
total_tickets = Ticket.objects.aggregate(Sum('quantity'))
total_cost = total_tickets * 25
Thank you Atul for your response. Is that supposed to be included right below the Ticket model or views? I tried to do that and it didn't go through. I tried this query: "total":Ticket.objects.filter(purchaser=User.objects.get(id=request.session['user_id'])).aggregate(total_cost=Sum(F('price')*F('quantity'), output_field=FloatField()) in views.py but it's invalid synthax. FYI, I included--default=25.00 in the price field--would these be correct? Thank you SO MUCH!
– Keiko
Apr 3 at 2:06
In views, you need to do this.
– Atul Kumar
Apr 3 at 3:38
add a comment |
More on aggregate
from django.db.models import Sum
# This will give total tickets sold to all the user
total_tickets = Ticket.objects.aggregate(Sum('quantity'))
total_cost = total_tickets * 25
More on aggregate
from django.db.models import Sum
# This will give total tickets sold to all the user
total_tickets = Ticket.objects.aggregate(Sum('quantity'))
total_cost = total_tickets * 25
answered Mar 31 at 10:39
Atul KumarAtul Kumar
3291 gold badge4 silver badges13 bronze badges
3291 gold badge4 silver badges13 bronze badges
Thank you Atul for your response. Is that supposed to be included right below the Ticket model or views? I tried to do that and it didn't go through. I tried this query: "total":Ticket.objects.filter(purchaser=User.objects.get(id=request.session['user_id'])).aggregate(total_cost=Sum(F('price')*F('quantity'), output_field=FloatField()) in views.py but it's invalid synthax. FYI, I included--default=25.00 in the price field--would these be correct? Thank you SO MUCH!
– Keiko
Apr 3 at 2:06
In views, you need to do this.
– Atul Kumar
Apr 3 at 3:38
add a comment |
Thank you Atul for your response. Is that supposed to be included right below the Ticket model or views? I tried to do that and it didn't go through. I tried this query: "total":Ticket.objects.filter(purchaser=User.objects.get(id=request.session['user_id'])).aggregate(total_cost=Sum(F('price')*F('quantity'), output_field=FloatField()) in views.py but it's invalid synthax. FYI, I included--default=25.00 in the price field--would these be correct? Thank you SO MUCH!
– Keiko
Apr 3 at 2:06
In views, you need to do this.
– Atul Kumar
Apr 3 at 3:38
Thank you Atul for your response. Is that supposed to be included right below the Ticket model or views? I tried to do that and it didn't go through. I tried this query: "total":Ticket.objects.filter(purchaser=User.objects.get(id=request.session['user_id'])).aggregate(total_cost=Sum(F('price')*F('quantity'), output_field=FloatField()) in views.py but it's invalid synthax. FYI, I included--default=25.00 in the price field--would these be correct? Thank you SO MUCH!
– Keiko
Apr 3 at 2:06
Thank you Atul for your response. Is that supposed to be included right below the Ticket model or views? I tried to do that and it didn't go through. I tried this query: "total":Ticket.objects.filter(purchaser=User.objects.get(id=request.session['user_id'])).aggregate(total_cost=Sum(F('price')*F('quantity'), output_field=FloatField()) in views.py but it's invalid synthax. FYI, I included--default=25.00 in the price field--would these be correct? Thank you SO MUCH!
– Keiko
Apr 3 at 2:06
In views, you need to do this.
– Atul Kumar
Apr 3 at 3:38
In views, you need to do this.
– Atul Kumar
Apr 3 at 3:38
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%2f55370737%2fhow-to-include-count-and-sum-query-sets-for-models-views%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
I think you're storing number of tickets purchased by a user in quantity ?
– Atul Kumar
Mar 29 at 10:13
Hi Atul, that is correct. I wanted to store the number or in this case the "quantity" of tickets times the fixed price of $25. I also wanted to see if I can add taxes to it. I tried adding a field of taxes as 0.0725. Can you please advise on how to go about querying it? I added: @property def total(self): q= self.price * self.quantity t= q *0.0725 totalPrice= t + self.price return totalPrice under Ticket model but to no avail. Thank you for your response, your feedback would be much appreciated!
– Keiko
Mar 30 at 19:03