Ben
Ben

Reputation: 21625

Django __init__() got an unexpected keyword argument using CreateView

I'm trying to override the get_form() method of the CreateView. My web page has two identical forms - one for adding a "registered" team and another for adding an "unregistered" team. If an unregistered team is being added, I want to set the team_name field of the form to "Available". As you can see in my code below, I tried accomplishing this by overriding the get_form() method

class TeamCreateView(LeagueContextMixin, CreateView):
    model = Team
    form_class = TeamForm
    template_name = "leagueapp/editleague.html"
    registered = False # the correct value of registered is passed in urls.py depending on the url that gets hit

    # Overwrite the get_success_url() method
    def get_success_url(self):
        return '/league/editleague/' + self.kwargs.get('league_id')

    def get_form(self, form_class):
        form_kwargs = self.get_form_kwargs()
        if not self.registered:
            form_kwargs['team_name'] = "Available"
        return form_class(**form_kwargs)

but this gives me the error __init__() got an unexpected keyword argument 'team_name'. What am I doing wrong and/or is there a better way to go about this?

Edit: This is my TeamForm

class TeamForm(ModelForm):
    class Meta:
        model = Team
        fields = ['team_name', 'captain', 'registered', 'team_location', 'phone', 'email', 'team_payment']
        widgets = {
            'team_name':TextInput(attrs={'class':'form-control input-md'}),
            'captain':TextInput(attrs={'class':'form-control input-md'}),
            'phone':TextInput(attrs={'class':'form-control input-md'}),
            'email':TextInput(attrs={'class':'form-control', 'type':'email'}),
            'team_location':TextInput(attrs={'class':'form-control input-md'}),
            'team_payment':TextInput(attrs={'class':'form-control'}),
            'registered':HiddenInput(),
        }

Upvotes: 0

Views: 1920

Answers (3)

Ben
Ben

Reputation: 21625

Finally figured out that I could change the get_form method like so:

def get_form(self, form_class):
    if self.registered:
        myform = super().get_form(form_class)
    else:
        myform = TeamForm({'team_name':'Available', 'team_payment':0.00, 'registered':False})
    return myform

Upvotes: 0

Daniel Roseman
Daniel Roseman

Reputation: 599628

You don't pass values to form initialization like that. It sounds like what you want to do is to provide custom initial data for the team name, so you should be updating the initial dict:

    if not self.registered:
        form_kwargs.setdefault('initial', {}).update(name="Available")

Upvotes: 1

Henrik Andersson
Henrik Andersson

Reputation: 47182

You've defined an __init__ method on TeamForm that does not allow for the argument team_name to be present which is why you get the TypeError when you unpack form_kwargs into that __init__(). Either update the __init__ to accept the new kwarg or rewrite the __init__ to

class TeamForm(forms.Form):
    def __init__(self, *args, **kwargs):
        #code

Upvotes: 1

Related Questions