More prefetching

-
This commit is contained in:
Oliver Walters 2019-05-21 00:06:57 +10:00
parent 6ae48d07c4
commit 157919f47a
2 changed files with 10 additions and 14 deletions

View File

@ -82,8 +82,10 @@ class QueryCountMiddleware(object):
n=len(connection.queries), n=len(connection.queries),
a=total_time, a=total_time,
b=(t_stop - t_start))) b=(t_stop - t_start)))
"""
for x in sorted(queries.items(), key=operator.itemgetter(1), reverse=True): for x in sorted(queries.items(), key=operator.itemgetter(1), reverse=True):
print(x[0], ':', x[1]) print(x[0], ':', x[1])
"""
return response return response

View File

@ -411,7 +411,7 @@ class Part(models.Model):
total = None total = None
# Calculate the minimum number of parts that can be built using each sub-part # Calculate the minimum number of parts that can be built using each sub-part
for item in self.bom_items.all(): for item in self.bom_items.all().select_related('sub_part'):
stock = item.sub_part.available_stock stock = item.sub_part.available_stock
n = int(1.0 * stock / item.quantity) n = int(1.0 * stock / item.quantity)
@ -449,7 +449,7 @@ class Part(models.Model):
builds = [] builds = []
for item in self.used_in.all(): for item in self.used_in.all().prefetch_related('part'):
for build in item.part.active_builds: for build in item.part.active_builds:
b = {} b = {}
@ -532,7 +532,7 @@ class Part(models.Model):
hash = hashlib.md5(str(self.id).encode()) hash = hashlib.md5(str(self.id).encode())
for item in self.bom_items.all(): for item in self.bom_items.all().prefetch('sub_part'):
hash.update(str(item.sub_part.id).encode()) hash.update(str(item.sub_part.id).encode())
hash.update(str(item.sub_part.full_name).encode()) hash.update(str(item.sub_part.full_name).encode())
hash.update(str(item.quantity).encode()) hash.update(str(item.quantity).encode())
@ -564,7 +564,7 @@ class Part(models.Model):
def required_parts(self): def required_parts(self):
""" Return a list of parts required to make this part (list of BOM items) """ """ Return a list of parts required to make this part (list of BOM items) """
parts = [] parts = []
for bom in self.bom_items.all(): for bom in self.bom_items.all().select_related('sub_part'):
parts.append(bom.sub_part) parts.append(bom.sub_part)
return parts return parts
@ -582,18 +582,12 @@ class Part(models.Model):
def has_complete_bom_pricing(self): def has_complete_bom_pricing(self):
""" Return true if there is pricing information for each item in the BOM. """ """ Return true if there is pricing information for each item in the BOM. """
for item in self.bom_items.all().prefetch_related('sub_part'): for item in self.bom_items.all().select_related('sub_part'):
if not item.sub_part.has_pricing_info: if not item.sub_part.has_pricing_info:
return False return False
return True return True
@property
def single_price_info(self):
""" Return a simplified pricing string for this part at single quantity """
return self.get_price_info()
def get_price_info(self, quantity=1, buy=True, bom=True): def get_price_info(self, quantity=1, buy=True, bom=True):
""" Return a simplified pricing string for this part """ Return a simplified pricing string for this part
@ -613,7 +607,7 @@ class Part(models.Model):
if min_price == max_price: if min_price == max_price:
return min_price return min_price
return "{a} to {b}".format(a=min_price, b=max_price) return "{a} - {b}".format(a=min_price, b=max_price)
def get_supplier_price_range(self, quantity=1): def get_supplier_price_range(self, quantity=1):
@ -650,7 +644,7 @@ class Part(models.Model):
min_price = None min_price = None
max_price = None max_price = None
for item in self.bom_items.all().prefetch_related('sub_part'): for item in self.bom_items.all().select_related('sub_part'):
prices = item.sub_part.get_price_range(quantity * item.quantity) prices = item.sub_part.get_price_range(quantity * item.quantity)
if prices is None: if prices is None: