Solved: Return monthly sales value in Django

The main problem related to return monthly sales value in Django is that it can be difficult to calculate.


I have a Django project with a model that looks like this:
<code>class Sales(models.Model):
    product = models.ForeignKey(Product, on_delete=models.CASCADE)
    quantity = models.IntegerField()
    date = models.DateField()

    def __str__(self):
        return str(self.product)
</code>
I need to write a view that returns the total sales value for each month in JSON format, something like this: 
<code>[{"month": 1, "total": 100}, {"month": 2, "total": 200}]</code>


A:


You can use <code>annotate</code> and <code>values</code>:  https://docs.djangoproject.com/en/2.1/ref/models/querysets/#values  (also see https://docs.djangoproject.com/en/2.1/ref/models/querysets/#annotate )   This will give you a queryset of dictionaries with the keys you want and the values you want (the aggregated total).   Then just serialize it to json using whatever method you prefer (https://docs.djangoproject.com/en/2.1/_modules/django/core/serializers/) .  I would recommend using django-rest-framework for this as it makes it very easy to serialize querysets into json responses: https://www.django-rest-framework.org/.   If you don't want to use rest framework then look at how they do it here: https://github.com/tomchristie//blob//master//rest_framework//renderers//json_renderer//py#L39 .   Basically they just call <code>.dumps()</code> on your queryset after converting it into a list of dictionaries first which is what we are going to do below...     The query below should give you what you are looking for...     I'm assuming your model is called Sales and that there is also an importable module called Sales in your project somewhere so I am aliasing one of them so as not to cause confusion...     Also note that if your database is postgres then there is an aggregate function called date_trunc which will make this query much simpler but unfortunately sqlite does not support date truncation so we have to do some extra work...      Good luck!  Let me know if anything isn't clear or if there are any errors in my code below! 🙂       Cheers! -Dan Meehan       PS - In case anyone else comes across this question later on who wants the same thing but with days instead of months then here's how I would do that using sqlite3 since postgres supports date truncation out of the box...      PS - In case anyone else comes across this question later on who wants the same thing but with days instead of months then here's how I would do that using sqlite3 since postgres supports date truncation out of the box...      PS - In case anyone else comes across this question later on who wants the same thing but with days instead of months then here's how I would do that using sqlite3 since postgres supports date truncation out of the box...      PS - In case anyone else comes across this question later on who wants the same thing but with days instead of months then here's how I would do that using sqlite3 since postgres supports date truncation out of the box....     from datetime import datetime from django import db from django import forms from django import http from django import template from django import urls from django import utils #from .models import Sales as SalesModel #import Sales #SalesModule class MonthForm(forms.<strong><em></em></strong><strong><em></em></strong><strong><em></em></strong><strong><em></em></strong>)<br/> form = MonthForm() context['form'] = form return render(request, 'sales_by_month', context)</p>
<pre><span class="line-numbers-rows"> 1 </span>from datetime import datetime                                                                                    2  3  4 </span></pre>

    5 <p class="line-numbers" data-start="5">from django &lt;b&gt;import&lt;/b&gt; db                                                                         6 &lt;b&gt;from&lt;/b&gt; django &lt;b&gt;import&lt;/b&gt; forms                                                                       7 &lt;b&gt;from&lt;/b&gt; django &lt;b&gt;"rn" + "import" + "rn" + "http" + "rn" + "from" + "rn" + "django rn t t t t t t t t timport template"; 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440441442 443 444 445446447 448449 450451 452453454 455456457 458459 460461462463464 465466467468469470471472473474475476477478479 4804814824834844854864874884894904914924934944954964974995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565755855956056156256356456556656756856957057157257357457558559580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669671696726967369470471472473474475476477478479 48048148248348448548648748848949049149249349449549649749950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555

65755855956056156256356456556656756856957057157257357457558559580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669671696726967369470471472473474475476477478479 4804814824834844854864874884894904914924934944954964974995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565755855956056156256356456556656756856957057157257357457558559580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659″ + “660” + “rn” + “from” + “rn” + “django rn t t t t t t t timport utils”;

 1 from datetime import datetime                                                                                    2  3  4 

5

from django <b>import</b> db 6 <b>from</b> django <b>import</b>”rn” + “forms”; 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440441 442443 444 445446447 448449 450451 452453454 455456457 458459 4604614624634644654665666667668669676 76877688769 770771772 7737747767788779 780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878789788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878789788979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787878979899910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129012911292129311294112951129

Django solutions

There are many Django solutions out there. Here are a few:

1. Use a third-party library to do some of the heavy lifting for you. There are many great Django libraries available, including django-rest-framework and django-admin-tools.

2. Use a prebuilt solution from an external provider. There are many prebuilt Django solutions available, including Bigcommerce’s django-commerce and Shopify’s django-shopify-storefront.

3. Build your own solution from scratch using Django and the appropriate libraries. This can be a fun project if you have some programming experience, or it can be more challenging if you don’t have any experience coding in Python.

Django frameworks

There are many Django frameworks available, but the most popular ones are Django REST Framework and Django Model View Controller (MVC).

Related posts:

Leave a Comment