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?
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.
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.
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.
taken = forms.ChoiceField(choices=TAKEN, label='Taken', widget=forms.Select)
you need to put the choices
in the choices
key
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With