Calculate max value of object field after model save

microspace

In views.py I have this

class CarListView(ListView):
model = Car

def get_context_data(self, **kwargs):
    context = super(CarListView, self).get_context_data(**kwargs)
    context['max_price'] = Car.objects.all().aggregate(Max('price'))
    return context 

Aggregate method will be called every time user requests the page. This will add some delay to page loading. I want this max_price variable to be calculated every time admin saved new Car objects. How to do this?

Arundas R

Performance in Django is of very much importance. If you want the requirement to be implemented in ListView go for

Car.objects.all().only('price').aggregate(Max('price'))

The sql query will be

SELECT MAX("table_car"."price") AS "price__max" FROM "table__car"

Here e are not fetching all the table data and so the performance will be much better than fetching all data.

You can use

Car.objects.all().values('price').aggregate(Max('price'))

also.

If you want to get the data when admin saves the data you can do this,

Car.objects.all().values('price').order_by('-price')[0]

This orders the car data based on values and fetches only one entry

SQL query:

SELECT "car"."price" FROM "car" ORDER BY "car__price"."price" DESC LIMIT 1

These will reduce SQL query time and will improve performance.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Save calculated value as a model field

From Dev

Display value in a field but don't save to model

From Dev

How to save Enum object with field value by jpa

From Dev

Django-model: Save computed value in a model field

From Dev

How to get the value of a Django Model Field object

From Dev

Django auto assign a value to a model field on_save in admin

From Dev

Save foreign key in django model by value of other field

From Dev

Get object IN a model with particular field value OR NOT IN a related model

From Dev

Update M2M value after model_save

From Dev

Not getting ForeignKey value after Model.Form.save

From Dev

Input field shows model value instead of label after select

From Dev

Save a list of objects in model as a field

From Dev

Rails: How do I save an object to a model after calling Model.new?

From Dev

Rails: How do I save an object to a model after calling Model.new?

From Dev

calculate max length of each field in csv file

From Dev

Reload Object after save()

From Dev

Calculate value from field ssql

From Dev

AngularJS - calculate value for each field

From Dev

Event After Every Model Save

From Dev

Unsigned field updated to max value

From Dev

Django script fails to save model instance, complains a field with a value is null...?

From Dev

Model object has no attribute save

From Dev

Model object has no attribute save

From Dev

Is there a need to set max_length in a model field?

From Dev

"no such column" after adding a field to the model

From Dev

Check previous model value on save

From Dev

Check previous model value on save

From Dev

Update field value in object

From Dev

How can I save the value of a custom field to wordpress' user_meta after altering it with javascript?

Related Related

  1. 1

    Save calculated value as a model field

  2. 2

    Display value in a field but don't save to model

  3. 3

    How to save Enum object with field value by jpa

  4. 4

    Django-model: Save computed value in a model field

  5. 5

    How to get the value of a Django Model Field object

  6. 6

    Django auto assign a value to a model field on_save in admin

  7. 7

    Save foreign key in django model by value of other field

  8. 8

    Get object IN a model with particular field value OR NOT IN a related model

  9. 9

    Update M2M value after model_save

  10. 10

    Not getting ForeignKey value after Model.Form.save

  11. 11

    Input field shows model value instead of label after select

  12. 12

    Save a list of objects in model as a field

  13. 13

    Rails: How do I save an object to a model after calling Model.new?

  14. 14

    Rails: How do I save an object to a model after calling Model.new?

  15. 15

    calculate max length of each field in csv file

  16. 16

    Reload Object after save()

  17. 17

    Calculate value from field ssql

  18. 18

    AngularJS - calculate value for each field

  19. 19

    Event After Every Model Save

  20. 20

    Unsigned field updated to max value

  21. 21

    Django script fails to save model instance, complains a field with a value is null...?

  22. 22

    Model object has no attribute save

  23. 23

    Model object has no attribute save

  24. 24

    Is there a need to set max_length in a model field?

  25. 25

    "no such column" after adding a field to the model

  26. 26

    Check previous model value on save

  27. 27

    Check previous model value on save

  28. 28

    Update field value in object

  29. 29

    How can I save the value of a custom field to wordpress' user_meta after altering it with javascript?

HotTag

Archive