Skip to content

Instantly share code, notes, and snippets.

@JakeAustwick
Last active August 29, 2015 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JakeAustwick/ab3a18aaaf5dcdc177b8 to your computer and use it in GitHub Desktop.
Save JakeAustwick/ab3a18aaaf5dcdc177b8 to your computer and use it in GitHub Desktop.
@db_session(retry=3)
def create_or_update_model(self, model, criteria, data):
"""
Creates a model with specified data if one doesn't match criteria, or updates if matches.
"""
# exists() was being annoying, work that out later
if model.select().filter(**criteria).count():
m = model.get(**criteria)
m.set(**data)
else:
data.update(criteria)
m = model(**data)
return m
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment