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;








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!










share|improve this question


























  • 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


















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!










share|improve this question


























  • 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














0












0








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!










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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


















  • 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













1 Answer
1






active

oldest

votes


















0














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





share|improve this answer

























  • 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










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%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









0














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





share|improve this answer

























  • 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















0














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





share|improve this answer

























  • 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













0












0








0







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





share|improve this answer













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






share|improve this answer












share|improve this answer



share|improve this answer










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

















  • 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








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%2f55370737%2fhow-to-include-count-and-sum-query-sets-for-models-views%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