Download:
child 119:2c3af104d654
parent 117:3fc42355adf8
118:71f8675cfaa9
Anton Shestakov <av6@dwimlabs.net>, Thu, 07 Jul 2016 13:31:08 +0800
models: manual_upsert() to create new instances or update existing

2 файлов изменено, 8 вставок(+), 4 удалений(-) [+]
candolint/models.py file | annotate | diff | comparison | revisions
incoming.py file | annotate | diff | comparison | revisions
--- a/candolint/models.py Wed Jul 06 21:21:47 2016 +0800
+++ b/candolint/models.py Thu Jul 07 13:31:08 2016 +0800
@@ -13,7 +13,7 @@
database = database
@classmethod
- def create_or_get(cls, **kwargs):
+ def manual_upsert(cls, **kwargs):
try:
with cls._meta.database.atomic():
return cls.create(**kwargs), True
@@ -28,7 +28,11 @@
field = getattr(cls, field_name)
if field.unique or field.primary_key or field_name in compound_unique:
query.append(field == value)
- return cls.get(*query), False
+ instance = cls.get(*query)
+ for field_name, value in kwargs.items():
+ setattr(instance, field_name, value)
+ instance.save()
+ return instance, False
class Project(BaseModel):
--- a/incoming.py Wed Jul 06 21:21:47 2016 +0800
+++ b/incoming.py Thu Jul 07 13:31:08 2016 +0800
@@ -167,13 +167,13 @@
domain, user, name = parse_project_url(url)
with database.transaction():
- project, created = Project.create_or_get(
+ project, created = Project.manual_upsert(
url=url,
domain=domain,
user=user,
name=name)
- change, created = Change.create_or_get(
+ change, created = Change.manual_upsert(
rev=rev,
node=node,
branch=branch,