Skip to content

Commit

Permalink
Avoid hitting the database twice for primary image
Browse files Browse the repository at this point in the history
  • Loading branch information
abbas123456 committed May 18, 2013
1 parent e0e2731 commit 7a0fa9f
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions oscar/apps/catalogue/abstract_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -453,15 +453,16 @@ def get_missing_image(self):

def primary_image(self):
images = self.images.all()
if images.count():
try:
return images[0]
# We return a dict with fields that mirror the key properties of the
# ProductImage class so this missing image can be used interchangably
# in templates. Strategy pattern ftw!
return {
'original': self.get_missing_image(),
'caption': '',
'is_missing': True}
except IndexError:
# We return a dict with fields that mirror the key properties of the
# ProductImage class so this missing image can be used interchangably
# in templates. Strategy pattern ftw!
return {
'original': self.get_missing_image(),
'caption': '',
'is_missing': True}

# Helpers

Expand Down

0 comments on commit 7a0fa9f

Please sign in to comment.