The create function under the hood looks more less like this:
def create(self, **data):
pass
As you can see you have one positional argument self
, other one is just a key words dictionary. When you call this function like this:
Product.objects.create(my_form.cleaned_data)
You are passing two positional arguments one is objects
this is how python handle classes and methods and other one is my_form.cleaned_data
, but the function exptes only one positional and any numbers of named arguments.
In second call:
Product.objects.create(**my_form.cleaned_data)
lets say the my_form.cleaned_data
looks like this:
{
'age': 1,
'name': 'good product'
}
so the equvialent of the second call would be
Product.objects.create(name='good product', age=1)
As you can see you have only one positional argument objects
and 2 named arguments.
In create function you can refer to data like this:
def create(self, **data):
name = data['name']
age = data['age']
objects.create
: docs.djangoproject.com/en/2.2/ref/models/querysets/#create It only takes keyword arguments in the form ofmodel_prop=value
and no positional arguments. The**
operator in python applies the dictionary as keyword arguments to the function. See #37401. – AlrightRawProductCreateForm
is aModelForm
, you could use the save method to create the Product object e.g.my_form.save()
– Haul