[Django]-Django model method – create_or_update

56👍

See QuerySet.update_or_create (new in Django 1.7dev)

132👍

There is update_or_create, eg::

obj, created = Person.objects.update_or_create(
    first_name='John', last_name='Lennon',
    defaults={'first_name': 'Bob'},
)
# If person exists with first_name='John' & last_name='Lennon' then update first_name='Bob'
# Else create new person with first_name='Bob' & last_name='Lennon'

Leave a comment