-
-
Save JakeAustwick/ab3a18aaaf5dcdc177b8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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