Ruslan
Ruslan

Reputation: 1301

Can not create new object - create() takes exactly 1 argument (2 given)

I am trying to create new object from values served from form. Here is the code:

def issue_save(request):
    issue = Issue.objects.create(IssueForm(request.POST))
    issue.save(commit=True)
    return HttpResponseRedirect(reverse("main.views.index"))

Here is the error I'm getting:

TypeError at /problemy/pridat/ulozit/
create() takes exactly 1 argument (2 given)
Request Method: POST
Request URL: http://localhost:8000/problemy/pridat/ulozit/
Django Version: 1.2.3
Exception Type: TypeError
Exception Value:
create() takes exactly 1 argument (2 given) Exception Location: views.py in issue_save, line 20

Code on line 20:

issue = Issue.objects.create(IssueForm(request.POST))

I'm really stuck here.

Upvotes: 2

Views: 3013

Answers (1)

Gabi Purcaru
Gabi Purcaru

Reputation: 31524

You didn't tell us what exactly is IssueForm. If it's a ModelForm, you can use its .save method instead:

IssueForm(request.POST).save()

If it's a regular form, use this:

issue = Issue.objects.create(**IssueForm(request.POST).cleaned_data)

Note: ** is argument unpacking (see here)

Upvotes: 3

Related Questions