1

I'm trying to run web app page which uses the below form;

class InputParametersForm(ModelForm):

    sqlConnection = SQLSeverConnection(
        'MSSQLServerDataSource',
        'default_user',
        'password123!!',
        'HD'
    )
    tableChoices = sqlConnection.getTableNames()
    TableName = forms.Select(
        widget=forms.Select(attrs={'class': 'selector'})
    )
    ColumnName = forms.Select(
        widget=forms.Select(attrs={'class': 'selector'})
    )
    StartDateTime = forms.DateField(
        widget=SelectDateWidget(
            empty_label=("Choose Year", "Choose Month", "Choose Day")
        )
    )
    EndDateTime = forms.DateField(
        widget=SelectDateWidget(
            empty_label=("Choose Year", "Choose Month", "Choose Day")
        )
    )

    class Meta:
        model = SelectionHistory
        fields = ("TableName", "ColumnName", "StartDateTime", "EndDateTime")

When I run manage.py runserver and go to the local URL I'm getting a 500 page with the error __init__() got an unexpected keyword argument 'widget' where I've tried to use the widget.

This is probably some basic error I'm making but if somebody could point me in the right direction it'd be a big help - preferably with some code.

markwalker_
  • 12,078
  • 7
  • 62
  • 99
Mark Corrigan
  • 544
  • 2
  • 11
  • 29

2 Answers2

2

Another possibility when receiving this error message is that Django has different types of fields when dealing with db models and form models. Make sure that your includes are in the correct order; include forms AFTER models. If you do something along the lines of:

from models import *
from django.forms import *

This will force the Form's field objects to be used instead of the Model's field objects which DO have the widget keyword.

Fydo
  • 1,396
  • 16
  • 29
0

forms.Select is a widget, it is not a Field and it doesn't have a widget argument. This is what the error is reporting about. This is what you basically have:

>>> from django import forms
>>> forms.Select(widget=forms.Select)
Traceback (most recent call last):
  File "<console>", line 1, in <module>
TypeError: __init__() got an unexpected keyword argument 'widget'

Instead, you meant to have a ChoiceField with a Select widget:

TableName = forms.ChoiceField(widget=forms.Select(attrs={'class': 'selector'}))

See also Daniel's example here:

Community
  • 1
  • 1
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195