10👍
Based on Keyword arguments’ documentation pointed out by @Ignacio Vazquez-Abrams
**kwargs allows you to handle named arguments that you have not
defined in advance.In a function call, keyword arguments must follow positional
arguments.All the keyword arguments passed must match one of the
arguments accepted by the function (e.g. actor is not a valid
argument for the parrot function), and their order is not
important.
1👍
Try this awesome explanation from digital ocean.
In summary, **kwargs is dict that holds parameters and can be used by first passing it through a view func e.g
def fun(req, **kwargs)
and then get values inside the function like this
kwargs.get('key_name')
.
- Django: Customize the User model's return field
- Annotate django query if filtered row exists in second table
- How to apply a filter backend to all fields of all resources in Django Rest Framework?
- Python gettext error: Can't convert '__proxy__' object to str implicitly
1👍
Check this article
def print_kwargs(**kwargs):
for key in kwargs:
print("The key {} holds {} value".format(key, kwargs[key]))
print_kwargs(a=1, b=2, c="Some Text")
Output:
The key a holds 1 value
The key b holds 2 value
The key c holds Some Text value
- Suppress "field should be unique" error in Django REST framework
- Django-allauth, JWT, Oauth
- Django-compressor: how to write to S3, read from CloudFront?
Source:stackexchange.com