0

I am trying to fill a field in a forms.ModelForm using a query based on a forms.Form. Unfortunately I am getting an AttributeError that suggests the field doesn't exist, and I'm not sure why this is.

The error is AttributeError: 'ElectionSuggestionForm' object has no attribute 'PostElection'

Here is the views.py:

def new_post(request):
    if request.method == 'POST':
        form = NewPostForm(request.POST)
        election_form = ElectionSuggestionForm(request.user, request.POST)
        if form.is_valid():
            post = form.save(commit=False)
            post.author = Candidate.objects.get(UserID=request.user, ElectionID=election_form.PostElection)
            post.save()
            return redirect('/feed/')
    else:    
        form = NewPostForm()
        election_form = ElectionSuggestionForm(request.user)
    return render(request, 'campaign/new_post.html', {
        "form": form,
        "election_form": election_form,
    })

Here is the forms.py:

class ElectionSuggestionForm(forms.Form):

    PostElection = forms.ModelChoiceField(queryset=None)

    def __init__(self, user, *args, **kwargs):
        super(ElectionSuggestionForm, self).__init__(*args, **kwargs)
        print(Election.objects.all().filter(candidate__UserID=user))
        self.fields['PostElection'].queryset = Election.objects.all().filter(candidate__UserID=user)

Thanks

cbuch1800
  • 923
  • 3
  • 11
  • 26

1 Answers1

2

to access the value of the PostElection attribute of the form you have to do it in the following way

election_form.cleaned_data['PostElection']

self.cleaned_data is an dictionary that receives all cleaned and validated data after calling the is_valid() method.

Make sure to call is_valid() on election_form as well.

def new_post(request):
    if request.method == 'POST':
        form = NewPostForm(request.POST)
        election_form = ElectionSuggestionForm(request.user, request.POST)
        if form.is_valid() and election_form.is_valid():
            post = form.save(commit=False)
            post.author = Candidate.objects.get(
                UserID=request.user, 
                ElectionID=election_form.cleaned_data['PostElection']
                )
            post.save()
            return redirect('/feed/')
    else:    
        form = NewPostForm()
        election_form = ElectionSuggestionForm(request.user)
    return render(request, 'campaign/new_post.html', {
        "form": form,
        "election_form": election_form,
    })
cbuch1800
  • 923
  • 3
  • 11
  • 26
  • `PostElection` is a field of `election_form`, not of `form`. And the `election_form` has to be validated too to get the `cleaned_data`. – bruno desthuilliers Jan 17 '18 at 09:07
  • Also - while this is indeed the way to get the form's data (minus the couple errors mentionned above), this does not explain the AttributeError on `election_form.PostElection` - the form class SHOULD have this attribute. – bruno desthuilliers Jan 17 '18 at 09:10
  • I have tried with one of my projects and if I try to access the form attribute like this: form_contact.message I get the error of `has no attribute`. Before refusing a help at least worthy to try the suggestion. Greetings. – Jimy Hendrix Falcón Cárdenas Jan 17 '18 at 09:21
  • I'm not the OP and I'm not "refusing" anything here, I just mentioned that your answer was partially broken (and still is ATM - you _do_ have to validate `election_form` to get the cleaned data) – bruno desthuilliers Jan 17 '18 at 09:34
  • wrt/ the AttributeError I can indeed reproduce the behaviour. It must have changed since last time I checked this (some years ago I have to admin) and the fields declared at the form class level were exposed directly as attributes of forms instances - which could cause some very funny bugs if you tried to modify `self.somefield` (instead of `self.fields["somefield"]`) in the initializer, hereby changed the form's field definition for the whole process xD – bruno desthuilliers Jan 17 '18 at 09:38
  • 1
    i change `form` by `election_form` and I have supported the reason for my response. – Jimy Hendrix Falcón Cárdenas Jan 17 '18 at 09:47