Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

__init__() got multiple values for argument 'user'

I have a form, model and view and trying to show ModelChoiceField with filters

I wrote an init in my forms.py but when im trying to submit my form on html page i got an error:

"__init__() got multiple values for argument ‘user’ "

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

forms.py

class WorkLogForm(forms.ModelForm):
    worklog_date = forms.DateField(label='Дата', widget=forms.DateInput(
        attrs={'class': 'form-control', 'placeholder': 'Введите дату'}))
    author = forms.EmailField(label='Автор',
                              widget=forms.EmailInput(attrs={'class': 'form-control', 'placeholder': 'Email автора'}))

    contractor_counter = forms.ModelChoiceField(queryset=CounterParty.objects.none())
    contractor_object = forms.ModelChoiceField(queryset=ObjectList.objects.none())
    contractor_section = forms.ModelChoiceField(queryset=SectionList.objects.none())

    description = forms.CharField(label='Описание',
                                  widget=forms.TextInput(attrs={'class': 'form-control', 'placeholder': 'Описание'}))

    def __init__(self, user, *args, **kwargs):
        super(WorkLogForm, self).__init__(*args, **kwargs)
        counter_queryset = CounterParty.objects.filter(counter_user=user)

        object_queryset = ObjectList.objects.filter(
            Q(customer_guid__in=counter_queryset) | Q(contractor_guid__in=counter_queryset))

        section_queryset = SectionList.objects.filter(object__in=object_queryset)

        self.fields['contractor_counter'].queryset = counter_queryset
        self.fields['contractor_object'].queryset = object_queryset
        self.fields['contractor_section'].queryset = section_queryset

    class Meta:
        model = WorkLog
        fields = (
            'worklog_date', 'author', 'contractor_counter', 'contractor_object', 'contractor_section', 'description')

views.py

def create_work_log(request):
    if request.method == 'POST':
        form = WorkLogForm(request.POST, user=request.user)
        if form.is_valid():
            form.author = request.user
            work_log = form.save()
            return render(request, 'common/home.html')
    else:
        form = WorkLogForm(user=request.user)

    return render(request, 'contractor/create_work_log.html', {'form': form})


>Solution :

In your forms init method you defined the parameters as:

def __init__(self, user, *args, **kwargs):

So your first parameter must be user instead of request.POST. So you can change

form = WorkLogForm(request.POST, user=request.user)

To

form = WorkLogForm(request.user, request.POST)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading