2👍
add_cat
function calls get_or_create
, which mean if your database has the same entry with name match, then just return the instance, otherwise create one. It returns a tuple. First element is the instance, second element is a boolean which indicates whether the result is a creation of new instance or not.
add_page
on the other hand, calls get_or_create
as well as save
, but it goes with the logic that add_page
needs. The save
in add_page
means:
“I have this page now, regardless if it’s a new page or an existing page, but I’m going to update the url
and the views
on it and save the result”. add_cat
only need to create Category
, not updating anything. Every time you update an existing instance, you need to call save
to save the changes.
Check django doc about get_or_create, it explains everything you need to know.