Skip to content

Instantly share code, notes, and snippets.

@ericflo
ericflo / get_and_update_or_create.py
Created January 2, 2012 20:21
Utility function for querying for and updating a model at the same time.
def get_and_update_or_create(model, unique, update):
"""
Given a model, a dictionary of lookup arguments, and a dictionary of update
arguments, this convenience function gets an object and updates it in the
database if necessary.
Returns a tuple (object, int) where int is 0 if the object was not updated,
1 if the object was created, and 2 if the object was updated in the
database.