Thursday, January 29, 2015

When do I have to call save() on a Model Instance?

I want to reduce the database queries, imagine I have a case like this:

def function1():
    user
= User.objects.get(id=1)
    user
.name ="Myself"
    function2
()
    function3
(user)
    user
.save()
   
def function2():
    user
= User.objects.get(id=1)
    user
.lastname = "private"
   
def function3(user):
    user
.age = 18

Even if this works (haven't tested it yet and I can imagine that at least function2() might be a problem), should I immeditately call .save() in each of those functions?

Or is it possible to freely modify the model and save it at a later stage? If so, function2() would not see the new value of user.name of course, however function3 should be able to see it (but probably not user.lastname).

Is there a best practice?

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/18ac4a4d-b1c2-4b36-af6c-5f0932a9ba2e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment