Skip to content

Commit

Permalink
ProductAttributesContainer: don't query for values if product is new
Browse files Browse the repository at this point in the history
  • Loading branch information
mvantellingen committed Aug 19, 2015
1 parent 8f50afc commit 752460f
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
8 changes: 6 additions & 2 deletions src/oscar/apps/catalogue/attributes.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def __init__(self, product):
self._initialised = False

def __getattr__(self, name):
if not name.startswith('_') and not self.initialised:
if not name.startswith('_') and not self._initialised:
self._load_values()
return getattr(self, name)
raise AttributeError(
Expand Down Expand Up @@ -75,7 +75,11 @@ def save(self):

# We don't filter on the attributes here since we assume all objects
# are still available in the orm cache.
value_objects = {v.attribute_id: v for v in self.get_values()}
if self._product_pk:
value_objects = {v.attribute_id: v for v in self.get_values()}
else:
value_objects = {}

for attribute in dirty_attributes:
new_value = getattr(self, attribute.code)
value_obj = value_objects.get(attribute.pk)
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/catalogue/models_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def test_save_attributes(self):
product.attr.text = 'foobar'
product.attr.integer = 1

with self.assertNumQueries(5):
with self.assertNumQueries(4):
product.save()

def test_load_attributes(self):
Expand Down

0 comments on commit 752460f

Please sign in to comment.