[PATCH 1/5] models: Make use of aggregates
Stephen Finucane
stephen at that.guru
Tue Oct 11 09:44:46 AEDT 2016
We're well past Django 1.1 now, so resolve a TODO to use aggregate
support introduced in this version. As part of this change, replace
the use of 'count' to check for presence of matching objects with
'exists'.
Signed-off-by: Stephen Finucane <stephen at that.guru>
---
patchwork/models.py | 22 ++++++++--------------
1 file changed, 8 insertions(+), 14 deletions(-)
diff --git a/patchwork/models.py b/patchwork/models.py
index f8759a5..2a98eff 100644
--- a/patchwork/models.py
+++ b/patchwork/models.py
@@ -577,25 +577,19 @@ class Bundle(models.Model):
return self.patches.order_by('bundlepatch__order')
def append_patch(self, patch):
- # todo: use the aggregate queries in django 1.1
- orders = BundlePatch.objects.filter(bundle=self).order_by('-order') \
- .values('order')
+ orders = BundlePatch.objects.filter(bundle=self).aggregate(
+ models.Max('order'))
- if len(orders) > 0:
- max_order = orders[0]['order']
+ if orders and orders['order__max']:
+ max_order = orders['order__max'] + 1
else:
- max_order = 0
+ max_order = 1
- # see if the patch is already in this bundle
- if BundlePatch.objects.filter(bundle=self,
- patch=patch).count():
+ if BundlePatch.objects.filter(bundle=self, patch=patch).exists():
return
- bp = BundlePatch.objects.create(bundle=self, patch=patch,
- order=max_order + 1)
- bp.save()
-
- return bp
+ return BundlePatch.objects.create(bundle=self, patch=patch,
+ order=max_order)
def public_url(self):
if not self.public:
--
2.7.4
More information about the Patchwork
mailing list