- orders = BundlePatch.objects.filter(bundle = self).values('order')
- max_order = max([ v for (k, v) in orders])
+ orders = BundlePatch.objects.filter(bundle = self).order_by('-order') \
+ .values('order')
+
+ if len(orders) > 0:
+ max_order = orders[0]['order']
+ else:
+ max_order = 0
+
+ # see if the patch is already in this bundle
+ if BundlePatch.objects.filter(bundle = self, patch = patch).count():
+ raise Exception("patch is already in bundle")