I want to use get_or_insert, But how do I know that it’s just created or fetched from existing entries.
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
In a nutshell, you can’t.
get_or_insertis just a convenience function, though, and one you can write and improve for yourself quite easily. Here’s a simple replacement that returns a tuple, indicating if it was created or not:And here’s how you’d use it:
Just bear in mind that if you use it with the recipe above, anything you do based on whether the entity is new or not is not transactional – your code could fail after creating the entity but before doing that. If what you want to do has side-effects, you should probably do it inside the transaction.