Django - get_or_create
Django's
get_or_create()
method either-
- gets an existing object with the given kwargs
- creates a new object if it doesn't exist
It returns a tuple containing an object and a boolean specifying whether a new object was created.
Visitor.objects.create(name="Harry", surname="Potter", age=16) visitor, created = Visitor.objects.get_or_create( name="Harry", surname="Potter", age=16 ) print(created) # => False visitor, created = Visitor.objects.get_or_create( name="Hermione", surname="Granger", age=16 ) print(created) # => True