from core.models import ConfigEntry

def get_config(name, user):
    """
    Gets the correct config value for the given key name.
    Value with the given user has priority over any default value.
    """
    if ConfigEntry.objects.filter(name=name, user=user).count() != 0:
        return ConfigEntry.objects.get(name=name, user=user)

    # No user value, look for global / default
    if ConfigEntry.objects.filter(name=name, user=None).count() != 0:
        return ConfigEntry.objects.get(name=name, user=None)
    else:
        raise KeyError("No configuration entry with key %s was found." % name)

from core.models import ConfigEntry

def get_config(name, user):
    """
    Gets the correct config value for the given key name.
    Value with the given user has priority over any default value.
    """
    try:
        return ConfigEntry.objects.get(name=name, user=user)
    except ConfigEntry.DoesNotExist:
        return ConfigEntry.objects.get(name=name, user=None)

Reduce queries used to lookup config
Reduce queries used to lookup config

  from core.models import ConfigEntry
  
  def get_config(name, user):
      """
      Gets the correct config value for the given key name.
      Value with the given user has priority over any default value.
      """
-     if ConfigEntry.objects.filter(name=name, user=user).count() != 0:
+     try:
          return ConfigEntry.objects.get(name=name, user=user)
+     except ConfigEntry.DoesNotExist:
+         return ConfigEntry.objects.get(name=name, user=None)
  
-     # No user value, look for global / default
-     if ConfigEntry.objects.filter(name=name, user=None).count() != 0:
-         return ConfigEntry.objects.get(name=name, user=None)
-     else:
-         raise KeyError("No configuration entry with key %s was found." % name)
- 
Reduce queries used to lookup config
## Code Before:
from core.models import ConfigEntry

def get_config(name, user):
    """
    Gets the correct config value for the given key name.
    Value with the given user has priority over any default value.
    """
    if ConfigEntry.objects.filter(name=name, user=user).count() != 0:
        return ConfigEntry.objects.get(name=name, user=user)

    # No user value, look for global / default
    if ConfigEntry.objects.filter(name=name, user=None).count() != 0:
        return ConfigEntry.objects.get(name=name, user=None)
    else:
        raise KeyError("No configuration entry with key %s was found." % name)

## Instruction:
Reduce queries used to lookup config
## Code After:
from core.models import ConfigEntry

def get_config(name, user):
    """
    Gets the correct config value for the given key name.
    Value with the given user has priority over any default value.
    """
    try:
        return ConfigEntry.objects.get(name=name, user=user)
    except ConfigEntry.DoesNotExist:
        return ConfigEntry.objects.get(name=name, user=None)
