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

19 views
Skip to first unread message

Tobias Dacoir

unread,
Jan 29, 2015, 11:43:44 AM1/29/15
to django...@googlegroups.com
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?

Vijay Khemlani

unread,
Jan 29, 2015, 12:10:01 PM1/29/15
to django...@googlegroups.com
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)

--
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...@googlegroups.com.
To post to this group, send email to django...@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.

Tobias Dacoir

unread,
Jan 29, 2015, 1:40:50 PM1/29/15
to django...@googlegroups.com
Thanks for answering all my questions.

So it's perfectly save to call the save method at a later time? As long as I keep the object and a pointer to it in memory I can freely modify it in several Functions before finally calling save at some point in time?

Vijay Khemlani

unread,
Jan 29, 2015, 1:57:21 PM1/29/15
to django...@googlegroups.com
yep, it's safe to do so

On Thu, Jan 29, 2015 at 3:40 PM, Tobias Dacoir <fal...@gmail.com> wrote:
Thanks for answering all my questions.

So it's perfectly save to call the save method at a later time? As long as I keep the object and a pointer to it in memory I can freely modify it in several Functions before finally calling save at some point in time?

--
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...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.

Erik Cederstrand

unread,
Jan 30, 2015, 3:45:41 AM1/30/15
to Django Users

> Den 29/01/2015 kl. 19.40 skrev Tobias Dacoir <fal...@gmail.com>:
>
> Thanks for answering all my questions.
>
> So it's perfectly save to call the save method at a later time? As long as I keep the object and a pointer to it in memory I can freely modify it in several Functions before finally calling save at some point in time?

This depends on your requirements. If there's a chance that another thread has modified the same object in the database from the time you fetch the data and until you call save(), then it's not necessarily safe. This would depend on your use of transactions, whether the data has been submitted from a form etc.

You should audit your code for these things if race conditions are a concern to you.

Erik
Reply all
Reply to author
Forward
0 new messages