Post #4443870
2026-08-08 09:21 UTC
g is for get_or_create
Django's get_or_create is great. It lets you search for something in the database and craete it if it didn't exist.
It's such a great tool for things like data entry and makes the code nice and easy to read and follow.
https://hamatti.org/posts/g-is-for-get-or-create-python-a-to-z/
#Python #Django #Blaugust #Blaugust2026
Replies (1)
-
@damien@layer8.space 2026-08-08 09:38
@hamatti@mastodon.world I encountered this for the first time a few days ago, and I'm confused about the use case for it - why would I ever need to get something that I already have? I.e. if I already have the data to be able to create (if it doesn't exist), doesn't that make the DB query pointless?