2

I ask myself this question, what the difference and the more performant between a method in models.py and views.py ?

Example 1:

models.py:

class Counter(models.Model):
    number = models.PositiveSmallIntegerField(default=0)

    def add_one(self):
        self.number += 1
        self.save()

views.py

from *xxx* import Counter

def count(request):
    c = Counter()
    c.add_one()
    c.save()
    return render(request, *xxx*)

Example 2:

models.py:

class Counter(models.Model):
    number = models.PositiveSmallIntegerField(default=0)

views.py

from *xxx* import Counter

def add_one(nb):
    nb += 1
    return nb

def count(request):
    c = Counter()
    c.number = add_one(c.number)
    return render(request, *xxx*)

My example is a little bit simple, but what the difference in the real life with big method and so many variables ?

Its have an impact on the performance of the server ? Did he have conventional or preference to choose one way ?

3
  • 1
    Why would there be any performance difference? Code is code, no matter where you put it. Commented Feb 27, 2017 at 11:16
  • 1
    The second one would be a lot faster since you never save it to the database, other than that they do the same exact thing Commented Feb 27, 2017 at 11:23
  • You can read here you have different possibility and it's to work on differente level on the framework and it's faster or not. And i try to find the good way to optimise my code. Commented Feb 27, 2017 at 12:45

1 Answer 1

4

both approaches are wrong!

The correct way is

Counter.objects.filter(pk=some_id).update(number=F('number')+1)

Note that this approach is needed to avoid race conditions. your current approach would need transactions to make it work properly. Code like the above typically go into the view.

Sign up to request clarification or add additional context in comments.

10 Comments

Thank you for the answer, but the incrementation was a simple example, in practice i do many process very repetitive with many treatment in the input of the users before to save it in database. So i know update is better, but i find an example very easy.
In pratice "yes" you reply to my example. But the question was just more theoretical : what the difference and the more performant between a method in models.py and views.py, if i understand you say view approach is better.
still both your comments are irrelevent. Because the code you have posted is simply the wrong way to do it. I suggest you post another question but before you do. Please read: stackoverflow.com/help/how-to-ask
I have to disagree on the "Code like the above typically go into the view" part - it really depends on what the effective code (not the dummy example provided by the OP) really do, how much of it is really domain model logic and as such should be encapsulated in the model etc. As far as I'm concerned I favor smart models and dumb views (the view should only handle the user->model interaction, not the model's inner logic).
yes, code is not just code for everyone. Realzing that means you are well on your way to success. All the best
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.