title: Django multiple lookup **kwargs
body: <p>I have the below function in django which works well, when just looking up based on a single lookup parameter (unique_id). However, I'm trying to get it work with multiple lookup parameters (for e.g. a table with a unique_together constraint which needs both fields in order to retrieve a unique result).  </p>
  
  <p>The previously working function is:</p>
  
  <pre><code>def create_or_update_if_diff(unique_id, defaults, model):
      """Helper function which is similar to update_or_create(), but will compare defaults to database entry
       and only update when there is any difference"""
      try:
          instance = model.objects.get(unique_id=unique_id)
      except model.DoesNotExist:
          instance = model.objects.create(unique_id=unique_id, **defaults)
          sys.stdout.write('New {} created: {}\n'.format(instance.__class__.__name__, instance.unique_id))
          return instance
      else:
          for key, value in defaults.items():
              attr = getattr(instance, key)
              if attr != value:
                  # If any change detected update all for efficiency
                  model.objects.filter(unique_id=unique_id).update(**defaults)
                  instance.refresh_from_db()
                  sys.stdout.write('{}: {} updated \n'.format(instance.__class__.__name__, instance.unique_id))
                  return instance
          return instance
  </code></pre>
  
  <p>Called with e.g. </p>
  
  <pre><code>prices = create_or_update_if_diff(unique_id=till_detail['Id'], defaults=defaults,
                                               model=Price)
  </code></pre>
  
  <p>The amended function is:</p>
  
  <pre><code>def create_or_update_if_diff(model, defaults=None, **lookup):
      """Helper function which is similar to update_or_create(), but will compare defaults to database entry
       and only update when there is any difference"""
      defaults = defaults or {}
      try:
          instance = model.objects.get(**lookup)
      except model.DoesNotExist:
          instance = model.objects.create(**lookup, **defaults)
          sys.stdout.write('New {} created: {}\n'.format(instance.__class__.__name__, instance.unique_id))
          return instance
      else:
          for key, value in defaults.items():
              attr = getattr(instance, key)
              if attr != value:
                  # If any change detected update all for efficiency
                  model.objects.filter(**lookup).update(**defaults)
                  instance.refresh_from_db()
                  sys.stdout.write('{}: {} updated \n'.format(instance.__class__.__name__, instance.unique_id))
                  return instance
          return instance
  </code></pre>
  
  <p>Which I tried to call with e.g. </p>
  
  <pre><code>lookup = {'name': default_product_string, 'supplier': supplier}
      default_product_instance = create_or_update_if_diff(lookup=lookup, defaults={'payment_method': payment_method_instance},
                                                             model=Product)
  </code></pre>
  
  <p>However I get the below error presumably because I have structured the function incorrently:
  <code>django.core.exceptions.FieldError: Cannot resolve keyword 'lookup' into field.</code></p>