Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When should the save method be called in Django?

Tags:

python

django

Should the save method be called after every create method or does calling the create method automatically call the save method?

If the save method is called automatically after creating an object then what would be a good use-case for the save method?

Thanks.

like image 350
Mridang Agarwalla Avatar asked Jun 30 '11 11:06

Mridang Agarwalla


People also ask

Is SAVE called on create Django?

create() will automatically save, so even if you fix your error - you will still have to make sure the arguments to create fulfill the database requirements to save a record.

Which method is used to save a model in DB in Python Django?

save() method is executed whenever an instance of a particular model is created either from admin interface or shell, save() method is run. You can override save() function before storing the data in the database to add new data or remove some data to be stored in a database.

How do I save models in Django?

Creating objects To create an object, instantiate it using keyword arguments to the model class, then call save() to save it to the database. This performs an INSERT SQL statement behind the scenes. Django doesn't hit the database until you explicitly call save() . The save() method has no return value.


1 Answers

No save() does not need to be called after create().

from the docs for create:

A convenience method for creating an object and saving it all in one step

its to be used in place of creating an object the normal way then saving with object.save()

like image 143
JamesO Avatar answered Nov 15 '22 08:11

JamesO