1👍
You did a mistake in sending kwargs
, this is how it works…
Lets create one sample lookup
dict..
>>> lookup = {'a': 1, 'b': 2}
Simulating create_or_update_if_diff
method…
>>> def create_or_update_if_diff(model, defaults=None, **lookup):
... print lookup
If you assign lookup=lookup
while calling the method….this mean you are creating one key word argument
called lookup
, ideally you are sending only one key word argument
>>> create_or_update_if_diff('model', lookup=lookup)
{'lookup': {'a': 1, 'b': 2}}
Since you model doesn’t have the
lookup
field, you are gettingFieldError
exception.
If you pass lookup
dict like below…you are sending two key word arguments a and b
to the method..
>>> create_or_update_if_diff('model', **lookup)
{'a': 1, 'b': 2}
So, the logic here is your lookup
dict {'a':1, 'b':2}
having two keys a and b
will be treated like below..
>>> create_or_update_if_diff('model', a=1, b=2)
{'a': 1, 'b': 2}
Hope this helps you to understand how **kwargs
works…
0👍
If you do lookup=lookup
as parameter, you would get {'lookup': {<your original dict>}}
in the function. I think you should just do in your function create_or_update_if_diff
:
lookup_params = lookup['lookup']
instance = model.objects.get(lookup_params)
- Django foreign key count filter
- Django 1.10 – how to load initial users
- Django Admin: models not showing if the user is not a superuser
0👍
You are not passing lookup
correctly, here is how it suppose to be:
lookup = {'name': default_product_string, 'supplier': supplier}
defaults = {'payment_method': payment_method_instance}
create_or_update_if_diff(defaults=defaults, model=Product, **lookup)
- Get instance from model to save to foreignkey field
- Serializer.is_valid() is always False
- Tastypie queryset values is not being displayed