Django - update_or_create
Django's
update_or_create()
method either-
- updates an existing object with the given kwargs along with a
defaults
dictionary of pairs for updating the object- 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.update_or_create( name="Harry", surname="Potter", defaults={"age": 21} ) print(visitor.age) # => '21' print(created) # => False