Django - best practice - calculating field values

I have a model in Django that has three fields that are computed based on the value of one field. For the value of one of the fields, in particular, records from another table for the query will be required (I will use the average of the last ten values).

I'm not sure where it is best to place this functionality in a model class as a model in a view?

Any advice would be appreciated - thanks

The model is as follows:

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)
+4
source share
3 answers

They should enter the model as methods (functions), here https://docs.djangoproject.com/en/1.8/topics/db/models/#model-methods , copied your example below

from django.db import models

class Person(models.Model):
  first_name = models.CharField(max_length=50)
  last_name = models.CharField(max_length=50)
  birth_date = models.DateField()

  def baby_boomer_status(self):
    "Returns the person baby-boomer status."
    import datetime
    if self.birth_date < datetime.date(1945, 8, 1):
        return "Pre-boomer"
    elif self.birth_date < datetime.date(1965, 1, 1):
        return "Baby boomer"
    else:
        return "Post-boomer"

  def _get_full_name(self):
    "Returns the person full name."
    return '%s %s' % (self.first_name, self.last_name)
  full_name = property(_get_full_name)

" - - ".

+1

( ), .

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)

, , , , " ".

decorator @cached_property, django docs

@cached_property . , , , , .

,

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 
+2

Well, since this is a question based on best practice, I think you are asking for suggestions.

If I were you, I would put this on signal in the model:

PD: Best practice call your models in camelcase SlideLibrary

from django.db.models.signals import pre_save

def calculate_values(sender, instance, *args, **kwargs):

    # Logic here to calculate fields

class SlideLibrary(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)

pre_save.connect(calculate_values, sender=SlideLibrary)
+1
source

Source: https://habr.com/ru/post/1614177/


All Articles