1 hour ago · Tech · hide · 0 comments

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 query for a specific item and if it doesn’t exist, it creates it. # Imagine we have a Card model from .models import Card pikachu_25, created = Card.objects.get_or_create( name='Pikachu', number=25 ) Here, get_or_create queries for a Card with name of Pikachu and number of 25. If it finds one, it returns a tuple of (card_found, False) . If one does not exist, it returns a tuple of (new_card, True). The…

No comments yet. Log in to reply on the Fediverse. Comments will appear here.