Thursday, January 29, 2015

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

You could pass the user as an optional parameter to function2.

Whether you should save the user or not in each of the methods depends on the logic of your application, or you can add a parameter to the method (True to save the object, False otherwise)

On Thu, Jan 29, 2015 at 1:43 PM, Tobias Dacoir <falc410@gmail.com> wrote:
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.

--
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/CALn3ei2bmfKq2k8cq2wBHrzM9FKWOapBR7ZL7PuDmtuu1%3DTN%3Dw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment