from django.core.exceptions import ObjectDoesNotExist


def get_attributes_display(variant, attributes):
    display = {}
    for attribute in attributes:
        value = variant.get_attribute(attribute.pk)
        if value:
            try:
                choice = attribute.values.get(pk=value)
            except ObjectDoesNotExist:
                pass
            except ValueError:
                display[attribute.pk] = value
            else:
                display[attribute.pk] = choice
    return display

def get_attributes_display(variant, attributes):
    display = {}
    for attribute in attributes:
        value = variant.get_attribute(attribute.pk)
        if value:
            choices = attribute.values.all()
            if choices:
                for choice in attribute.values.all():
                    if choice.pk == value:
                        display[attribute.pk] = choice
                        break
            else:
                display[attribute.pk] = value
    return display

Reduce number of queries of getting attributes display
Reduce number of queries of getting attributes display

- from django.core.exceptions import ObjectDoesNotExist
- 
- 
  def get_attributes_display(variant, attributes):
      display = {}
      for attribute in attributes:
          value = variant.get_attribute(attribute.pk)
          if value:
-             try:
-                 choice = attribute.values.get(pk=value)
+             choices = attribute.values.all()
-             except ObjectDoesNotExist:
-                 pass
-             except ValueError:
+             if choices:
+                 for choice in attribute.values.all():
+                     if choice.pk == value:
+                         display[attribute.pk] = choice
+                         break
+             else:
                  display[attribute.pk] = value
-             else:
-                 display[attribute.pk] = choice
      return display
  
Reduce number of queries of getting attributes display
## Code Before:
from django.core.exceptions import ObjectDoesNotExist


def get_attributes_display(variant, attributes):
    display = {}
    for attribute in attributes:
        value = variant.get_attribute(attribute.pk)
        if value:
            try:
                choice = attribute.values.get(pk=value)
            except ObjectDoesNotExist:
                pass
            except ValueError:
                display[attribute.pk] = value
            else:
                display[attribute.pk] = choice
    return display

## Instruction:
Reduce number of queries of getting attributes display
## Code After:
def get_attributes_display(variant, attributes):
    display = {}
    for attribute in attributes:
        value = variant.get_attribute(attribute.pk)
        if value:
            choices = attribute.values.all()
            if choices:
                for choice in attribute.values.all():
                    if choice.pk == value:
                        display[attribute.pk] = choice
                        break
            else:
                display[attribute.pk] = value
    return display
