Where to place mathematical models in Django

Asked

Viewed 92 times

1

I’m new to the subject, so forgive the ignorance.

I am developing an application using python in Django, where from user informed data the application will return certain values.

But I am in doubt where I put my mathematical formulas. If I can put inside the models.py folder and create a function within the class.. for example:

class Consumo(models.Model):
    media_anual = models.CharField('Média anual de consumo', max_length=5)
    (...)
    
    def formula1:
    (...)

if anyone can help, thank you

1 answer

1


I adopt the practice of putting in the models the methods that depend on some kind of interaction with Django and with the database, there are queries and preparation of parameters.

The calculations, formulas, etc. I put in other files, and these files are "pure" without elements of Django. These functions (or methods) receive parameters, but never models.

I adopted this practice because it facilitates development in general, debug and implementation of unit tests. For example, if I have a function that performs a certain calculation I can develop and test it directly by passing test parameters, without having to import Django (which is time consuming), without having to deal with Django dependencies and without having to deal with models, make mocks and things like that.

  • Thank you so much! I’ll do it this way, thank you so much

Browser other questions tagged

You are not signed in. Login or sign up in order to post.