Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django - what is best practice - Calculating field values [closed]

Tags:

python

django

I have a model in Django which has three fields which are calculated based on the value of one field. The value of one of the fields in particular will require records from another table to be queried ( I will be using the average value of the last ten values).

I am unsure as to where is the best place to put this functionality, in the model class, in a model form, in a view?

Any advice would be appreciated - thanks

The model looks like this:

class slide_library(models.Model):

    slide_name = models.Charfield(max_length = 6, primary_key = True)
    reference_value = models.FloatField(default= '0')
    last_mean = models.FloatField(default= '0')
    esd = models.FloatField(default= '0')
    criteria = models.Charfield(max_length= 10)
like image 980
Chas Avatar asked Nov 02 '15 17:11

Chas


People also ask

What is the purpose of calculated fields?

A calculated field is a field that uses existing database fields and applies additional logic — it allows you to create new data from your existing data. A calculated field either: performs some calculation on database fields to create a value that is not directly stored in the database or.

What is the purpose of __ Str__ method in Django?

str function in a django model returns a string that is exactly rendered as the display name of instances for that model.

What is @property Django?

What is @property in Django? Here is how I understand it: @property is a decorator for methods in a class that gets the value in the method.

What is required field in Django model?

How to use required in Django Form field? required is often used to make the field optional that is the user would no longer be required to enter the data into that field and it will still be accepted.


1 Answers

You can avoid using signals(you should use signals as last resource), overriding model save method and calculating your values before store the instance.

class slide_library(models.Model):

    slide_name = models.Charfield(max_length = 6, primary_key = True)
    reference_value = models.FloatField(default= '0')
    last_mean = models.FloatField(default= '0')
    esd = models.FloatField(default= '0')
    criteria = models.Charfield(max_length= 10)

    #Overriding
    def save(self, *args, **kwargs):
        #set here your calculated attributes
        self.my_stuff = 'something I want to save in that field'
        super(slide_library, self).save(*args, **kwargs)

Also, if they are calculated attributes, think if you really need to store it in DB, you can calculate on the fly.

You can use decorator @cached_property, as django docs says

The @cached_property decorator caches the result of a method with a single self argument as a property. The cached result will persist as long as the instance does, so if the instance is passed around and the function subsequently invoked, the cached result will be returned.

Consider a typical case, where a view might need to call a model’s method to perform some computation, before placing the model instance into the context

from django.utils.functional import cached_property

class slide_library(models.Model):

    slide_name = models.Charfield(max_length = 6, primary_key = True)
    reference_value = models.FloatField(default= '0')
    last_mean = models.FloatField(default= '0')
    esd = models.FloatField(default= '0')
    criteria = models.Charfield(max_length= 10)

    @cached_property
    def derivate_field_1(self):
        #Here goes all ur stuff to calculated your field
        value = ....your calculated value
        return value 
like image 90
levi Avatar answered Oct 02 '22 13:10

levi