Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django TypeError: __init__() takes 1 positional argument but 2 were given

Tags:

python

django

I can't find solution for the TypeError: init() takes 1 positional argument but 2 were given

TypeError appears when I try to make migrations in ubuntu concole. error:

  File "/home/wojciech/workspace/conference_room/conference_room/conference/forms.py", line 8, in NewRoomForm
    taken = forms.ChoiceField(TAKEN, label='Taken', widget=forms.Select)
TypeError: __init__() takes 1 positional argument but 2 were given

forms:

from django import forms
from .models import TAKEN


class NewRoomForm(forms.Form):
    name = forms.CharField(label='Name', max_length=32)
    number = forms.IntegerField(label='Room Number')
    taken = forms.ChoiceField(TAKEN, label='Taken', widget=forms.Select)
    description = forms.CharField(label='Description', widget=forms.Textarea)

models:

TAKEN = (
    (True, 'Yes'),
    (False, 'No')
)


class Room(models.Model):
    name = models.CharField(max_length=32)
    number = models.IntegerField()
    taken = models.BooleanField(choices=TAKEN)
    description = models.CharField(max_length=128)

Any ideas how to fix it?

like image 501
wojo44 Avatar asked Mar 21 '18 06:03

wojo44


People also ask

How Do You Solve takes 1 positional argument but 2 were given?

To solve this Typeerror: takes 1 positional argument but 2 were given is by adding self argument for each method inside the class. It will remove the error. Taking the same example if I will execute the below lines of code then I will not get the error.

Where () takes 2 positional arguments but 3 were given?

The Python "TypeError: takes 2 positional argument but 3 were given" occurs for multiple reasons: Forgetting to specify the self argument in a class method. Forgetting to specify a third argument in a function's definition. Passing three arguments to a function that only takes two.

What is positional argument error in Python?

The Python rule says positional arguments must appear first, followed by the keyword arguments if we are using it together to call the method. The SyntaxError: positional argument follows keyword argument means we have failed to follow the rules of Python while writing a code.


Video Answer


1 Answers

taken = forms.ChoiceField(choices=TAKEN, label='Taken', widget=forms.Select)

you need to put the choices in the choices key

like image 88
Exprator Avatar answered Sep 18 '22 17:09

Exprator