G is for get_or_create - Python A to Z Dark _ □ ✕
  • Posts
  • About
  • Debug
Post

G is for get_or_create - Python A to Z

Published
2026-08-08 00:00 UTC (precision: second)
Source feed
Juha-Matti Santala - Community Builder. Dreamer. Adventurer. excluded
Link
https://hamatti.org/posts/g-is-for-get-or-create-python-a-to-z/
Canonical
https://hamatti.org/posts/g-is-for-get-or-create-python-a-to-z
Length
2072 runes
Extracted text
Python A-Z is a blog series about Python. Each day, I share insights, ideas and examples for different parts of Python development that match with the letter of the day. Blaugust is an annual blogging festival in August where the goal is to write a blog post every day of the month. Let’s take our first journey into the wonders of Django, my favourite web backend. One of the reasons I love it is its ORM which we’ll talk a bit more when we reach the midway of the month and letter O. Today, I want to show my appreciation for one of the methods in QuerySet: get_or_create which allows you to create new items in the database but if one already exists, it returns it instead. from .models import Bread, BreadType # (here, Bread.bread_type is a ForeignKey to BreadType) breads_input = [{ 'id': 1, 'breadType': 'bun' } , ... ] for bread_data in breads: bread_type, type_created = BreadType.objects.get_or_create(type=bread_data['breadType']) bread, bread_created = Bread.objects.get_or_create(id=bread_data['id'], bread_type=bread_type) Here we process through some list of dictionaries (imagine it’s read from a JSON file or queried from a REST API). First, we try to create a new BreadType but if one already exists, we return a reference to it rather than creating a new one every time. Then we create a bread, using the previous reference. If we’d already have that specific bread inputted (maybe we ran the command twice), we want to get the existing one rather than creating new ones every time. I especially love using it in my data entry scripts / custom Django commands. Many of my applications operate on a model where I periodically input new data through these commands: for example in Pokémon TCG app, when a new set is released. Pokémon TCG is a great example. Right now, when my script reads in data for new cards, there are 13 such relationships between my Card model and other models. The codebase looks so much neater and easier to follow thanks to get_or_create. Edit later on Aug 8th: I rewrote the entire post to better explain what get_or_create does.
Stored topics

Every stored assignment is listed, including rows below the current cutoff (0.75), so a missing tag can be explained.

Concept Code Score Meter Margin Origin Path
economy, business and finance economy-business-and-finance 0.96 3.06 ↑derived from 20000225 economy, business and finance
products and services products-and-services 0.96 3.06 ↑derived from 20000225 economy, business and finance > products and services
computing and information technology computing-and-information-technology 0.96 3.06 direct economy, business and finance > products and services > computing and information technology
science and technology science-and-technology 0.95 2.99 ↑derived from 20000763 science and technology
technology and engineering technology-and-engineering 0.95 2.99 ↑derived from 20000763 science and technology > technology and engineering
information technology and computer science information-technology-and-computer 0.95 2.99 direct science and technology > technology and engineering > information technology and computer science
arts, culture, entertainment and media arts-culture-entertainment-and-media 0.94 2.74 ↑derived from local:blogging arts, culture, entertainment and media
mass media mass-media 0.94 2.74 ↑derived from local:blogging arts, culture, entertainment and media > mass media
social media social-media 0.94 2.74 ↑derived from local:blogging arts, culture, entertainment and media > mass media > social media
Blogging blogging 0.94 2.74 direct arts, culture, entertainment and media > mass media > social media > Blogging
software and applications software-and-applications 0.90 2.19 ↑derived from local:software-development economy, business and finance > products and services > computing and information technology > software and applications
Software development software-development 0.90 2.19 direct economy, business and finance > products and services > computing and information technology > software and applications > Software development
Open source open-source 0.78 1.29 direct economy, business and finance > products and services > computing and information technology > software and applications > Open source
artificial intelligence artificial-intelligence 0.28 -0.97 direct science and technology > technology and engineering > information technology and computer science > artificial intelligence
lifestyle and leisure lifestyle-and-leisure 0.20 -1.37 ↑derived from 20000538 lifestyle and leisure
leisure leisure 0.20 -1.37 direct lifestyle and leisure > leisure
hobby hobby 0.16 -1.68 direct lifestyle and leisure > leisure > hobby
Information security information-security 0.12 -2.01 direct science and technology > technology and engineering > information technology and computer science > Information security
game game 0.04 -3.25 ↑derived from local:mmorpg lifestyle and leisure > leisure > game
video game video-game 0.04 -3.25 ↑derived from local:mmorpg lifestyle and leisure > leisure > game > video game
MMORPG mmorpg 0.04 -3.25 direct lifestyle and leisure > leisure > game > video game > MMORPG
Tabletop gaming tabletop-gaming 0.04 -3.29 direct lifestyle and leisure > leisure > game > Tabletop gaming
board game board-game 0.03 -3.50 direct lifestyle and leisure > leisure > game > board game
human interest human-interest 0.03 -3.51 direct human interest
society society 0.03 -3.54 ↑derived from 20001300 society
fundamental rights fundamental-rights 0.03 -3.54 ↑derived from 20001300 society > fundamental rights
privacy privacy 0.03 -3.54 direct society > fundamental rights > privacy
card game card-game 0.03 -3.59 direct lifestyle and leisure > leisure > game > card game
arts and entertainment arts-and-entertainment 0.02 -4.04 ↑derived from 20000013 arts, culture, entertainment and media > arts and entertainment
literature literature 0.02 -4.04 direct arts, culture, entertainment and media > arts and entertainment > literature
media and entertainment industry media-and-entertainment-industry 0.01 -4.20 ↑derived from 20000306 economy, business and finance > products and services > media and entertainment industry
books and publishing books-and-publishing 0.01 -4.20 direct economy, business and finance > products and services > media and entertainment industry > books and publishing
environment environment 0.01 -4.33 direct environment
sport sport 0.01 -4.37 direct sport
social sciences social-sciences 0.01 -4.55 direct science and technology > social sciences
culture culture 0.01 -4.77 direct arts, culture, entertainment and media > culture
labour labour 0.01 -4.80 direct labour
film industry film-industry 0.01 -4.84 direct economy, business and finance > products and services > media and entertainment industry > film industry
conflict, war and peace conflict-war-and-peace 0.01 -4.84 direct conflict, war and peace
award and prize award-and-prize 0.01 -4.85 direct human interest > award and prize
Self-hosting self-hosting 0.01 -5.09 direct economy, business and finance > products and services > computing and information technology > Self-hosting
competition discipline competition-discipline 0.01 -5.17 ↑derived from 20001183 sport > competition discipline
eSports esports 0.01 -5.17 direct sport > competition discipline > eSports
streaming service streaming-service 0.01 -5.22 direct economy, business and finance > products and services > media and entertainment industry > streaming service
crime, law and justice crime-law-and-justice 0.00 -5.49 ↑derived from 20000086 crime, law and justice
crime crime 0.00 -5.49 ↑derived from 20000086 crime, law and justice > crime
cyber crime cyber-crime 0.00 -5.49 direct crime, law and justice > crime > cyber crime
health health 0.00 -5.53 ↑derived from 20000464 health
health treatment and procedure health-treatment-and-procedure 0.00 -5.53 direct health > health treatment and procedure
family family 0.00 -5.54 direct society > family
weather weather 0.00 -5.65 ↑derived from 20001128 weather
weather forecast weather-forecast 0.00 -5.65 direct weather > weather forecast
disease and condition disease-and-condition 0.00 -5.76 ↑derived from 20000458 health > disease and condition
mental health and disorder mental-health-and-disorder 0.00 -5.76 direct health > disease and condition > mental health and disorder
religion religion 0.00 -5.76 direct religion
animation animation 0.00 -5.87 direct arts, culture, entertainment and media > arts and entertainment > animation
exercise and fitness exercise-and-fitness 0.00 -5.91 direct lifestyle and leisure > wellness > exercise and fitness
wellness wellness 0.00 -5.91 ↑derived from 20001239 lifestyle and leisure > wellness
disaster, accident and emergency incident disaster-accident-and-emergency-incident 0.00 -5.94 direct disaster, accident and emergency incident
public health public-health 0.00 -5.96 direct health > public health
natural science natural-science 0.00 -6.01 direct science and technology > natural science
education education 0.00 -6.02 direct education
podcast podcast 0.00 -6.07 direct economy, business and finance > products and services > media and entertainment industry > podcast
Cryptography cryptography 0.00 -6.09 direct science and technology > technology and engineering > information technology and computer science > Cryptography
scientific research scientific-research 0.00 -6.15 ↑derived from 20000739 science and technology > scientific research
scientific exploration scientific-exploration 0.00 -6.15 ↑derived from 20000739 science and technology > scientific research > scientific exploration
space exploration space-exploration 0.00 -6.15 direct science and technology > scientific research > scientific exploration > space exploration
music music 0.00 -6.32 direct arts, culture, entertainment and media > arts and entertainment > music
politics and government politics-and-government 0.00 -6.33 direct politics and government
diet diet 0.00 -6.41 direct health > health treatment and procedure > diet
history history 0.00 -6.50 direct science and technology > social sciences > history
climate change climate-change 0.00 -6.57 direct environment > climate change
television television 0.00 -6.64 direct arts, culture, entertainment and media > mass media > television
biology biology 0.00 -6.69 direct science and technology > natural science > biology
food and drink enthusiasm food-and-drink-enthusiasm 0.00 -7.15 direct lifestyle and leisure > leisure > hobby > food and drink enthusiasm
consumer goods consumer-goods 0.00 -7.46 ↑derived from 20001160 economy, business and finance > products and services > consumer goods
handicrafts handicrafts 0.00 -7.46 direct economy, business and finance > products and services > consumer goods > handicrafts
visual arts visual-arts 0.00 -7.68 ↑derived from 20000036 arts, culture, entertainment and media > arts and entertainment > visual arts
photography photography 0.00 -7.68 direct arts, culture, entertainment and media > arts and entertainment > visual arts > photography
travel and tourism travel-and-tourism 0.00 -8.28 direct lifestyle and leisure > leisure > travel and tourism
lifestyle lifestyle 0.00 -8.96 ↑derived from 20001257 lifestyle and leisure > lifestyle
house and home house-and-home 0.00 -8.96 ↑derived from 20001257 lifestyle and leisure > lifestyle > house and home
gardening gardening 0.00 -8.96 direct lifestyle and leisure > lifestyle > house and home > gardening

← Back to posts

feedtagger 0.1.0-dev model: modernbert-base-zeroshot-v2.0/int8@all-s256 651 posts 0 unclassified min score 0.75