[Fixed]-Is there any reason to use "if not ***.filter().exists(): ***.create()" instead of "***.get_or_create()"?

1👍

Well get_or_create is

A convenience method for looking up an object with the given kwargs
(may be empty if your model has defaults for all fields), creating one
if necessary.

So at the very least you are violating the DRY principal. Going beyond that you are checking for all the fields in the model not just the primary key or unique field. You are try to find an object where all the fields match. So chances are very high that you will get false results and your create statement will fail. That’s why get_or_create has the defaults parameter

👤e4c5

Leave a comment