V
V
Vitaly Ananiev2022-01-07 19:09:03
Django
Vitaly Ananiev, 2022-01-07 19:09:03

Why does 'form': AddAvailabilityForm(initial={'name': games}), TypeError: __init__() missing 1 required positional argument: 'name' error?

Why does 'form': AddAvailabilityForm(initial={'name': games}),
TypeError: __init__() missing 1 required positional argument: 'name'

forms.py

class AddAvailabilityForm(forms.ModelForm):
    def __init__(self, name, *args, **kwargs):
        super(AddAvailabilityForm, self).__init__(*args, **kwargs)
        self.fields['name'].queryset = Test.objects.filter(name=name)

    class Meta:
        model = Test

        fields = ['name', 'id_num']

        widgets = {
            'name': forms.Select(),
            'id_num': forms.Select()
        }
views.py<code lang="python">
class SellAvailabilityDetail(View):
    def get(self, request, slug):
        games = Test.objects.get(id_num=slug)
        if request.POST:
            form = AddAvailabilityForm(request.POST)
            if form.is_valid():
                form.save()
                return redirect('base')
        context = {
            'form': AddAvailabilityForm(initial={'name': games}), - причем вот здесь скобку подчеркивает желтым
            'games': games
        }
        return render(request, 'Pay/sell_availability_detail.html', context)
</code>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
Kadabrov, 2022-01-07
@Kadabrov

because name is a required class argument

class AddAvailabilityForm(forms.ModelForm):
    def __init__(self, name, *args, **kwargs):

Functions and their arguments
Class constructor - __init__() method

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question