X-Git-Url: http://git.veekun.com/zzz-floof.git/blobdiff_plain/3bdc7971c3359efe7f38eb1b94253b416471b87b..570a60f8364dbd148fbd7b903a4422acbba6dfbc:/floof/lib/dbhelpers.py?ds=sidebyside diff --git a/floof/lib/dbhelpers.py b/floof/lib/dbhelpers.py index 4d10030..23fd3b4 100644 --- a/floof/lib/dbhelpers.py +++ b/floof/lib/dbhelpers.py @@ -2,4 +2,18 @@ def find_or_create(model, **kwargs): instance = model.get_by(**kwargs) if not instance: instance = model(**kwargs) + return instance + +def update_or_create(model, get_by, update_with): + instance = model.get_by(**get_by) + if instance: + # set new values + for key,value in update_with.items(): + setattr(instance, key, value) + else: + # create it + both = {} + both.update(get_by) + both.update(update_with) + instance = model(**both) return instance \ No newline at end of file