Help with: Django 'ModelForm' object has no attribute 'cleaned_data' for M2M through

4,140 views
Skip to first unread message

Ronaldo Bahia

unread,
Apr 2, 2015, 12:36:29 PM4/2/15
to django...@googlegroups.com

Vijay Khemlani

unread,
Apr 2, 2015, 12:44:44 PM4/2/15
to django...@googlegroups.com
Call form.is_valid() (and check that it returns True) before accessing form.cleaned_data

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/65204e73-37e5-4463-862f-cf453d835440%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Ronaldo Bahia

unread,
Apr 2, 2015, 1:17:11 PM4/2/15
to django...@googlegroups.com
Thanks. Solved cleaned_data issue.

Now I getting:

Cannot assign "u'2'": "CandidateToJob.job" must be a "Job" instance.

Or

Cannot assign "u'Title'": "CandidateToJob.job" must be a "Job" instance.

Vijay Khemlani

unread,
Apr 2, 2015, 1:51:11 PM4/2/15
to django...@googlegroups.com
In your Form class "job" is a ChoiceField, and it returns a primitive value (such as "2") when cleaned.

You would need to convert it back to a django model with

job = Job.objects.get(pk=form.cleaned_data['job'])



Ronaldo Bahia

unread,
Apr 2, 2015, 4:12:17 PM4/2/15
to django...@googlegroups.com
Dont know why but I still get the same error

Ronaldo Bahia
You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/Ve_RLvkK4Gs/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-users...@googlegroups.com.

To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.

Vijay Khemlani

unread,
Apr 2, 2015, 4:16:06 PM4/2/15
to django...@googlegroups.com
post your updated code

Ronaldo Bahia

unread,
Apr 2, 2015, 4:24:30 PM4/2/15
to django...@googlegroups.com

Vijay Khemlani

unread,
Apr 2, 2015, 4:28:42 PM4/2/15
to django...@googlegroups.com
What line is thrwing the error? the one with

form.save(commit=False)

?

Ronaldo Bahia

unread,
Apr 2, 2015, 4:40:06 PM4/2/15
to django...@googlegroups.com
No.
This line:

job = Job.objects.get(id=form.cleaned_data['job'])
If I change my form to get job__id:

cojobs=Company.objects.select_related('job__id').values_list('job__id', flat=True)


The error is still that line but a different message:

Job matching query does not exist.



Ronaldo Bahia 
Diretor | JobConvo.com
Tel: 11 3280 6971

Ronaldo Bahia

unread,
Apr 2, 2015, 4:42:10 PM4/2/15
to django...@googlegroups.com
Sorry. It is 

if form.is_valid():


The line providing the error

Ronaldo Bahia 
Diretor | JobConvo.com
Tel: 11 3280 6971

Ronaldo Bahia

unread,
Apr 4, 2015, 11:49:01 AM4/4/15
to django...@googlegroups.com

Ronaldo Bahia

unread,
Apr 6, 2015, 1:11:03 AM4/6/15
to django...@googlegroups.com, Vijay Khemlani

One more thing. The Job form field is showing the jobs from all companies. But I ned to filter only the jobs published by the user which is logged in.

I've tried but couldn't make it happen.

Can you tell how to do it?

Thanks

#forms.py
class SendCandForm(forms.Form):

    job = Company.objects.select_related('job').values_list('job__title', flat=True)
    jobs_choices = [('', 'Escolher')] + [(id, id) for id in job]

    job = forms.ChoiceField(choices=jobs_choices, label="Vaga", required=True, widget=forms.Select(attrs={'class': 'form-control'}))

    STATUS_CHOICES = (
        ('0', 'Novo'),
        ('1', 'Não aprovado'),
        ('2', 'Pré Selecionado'),
        ('3', 'Contratado')
    )
    status = forms.ChoiceField(required=True, widget=forms.RadioSelect(attrs={'class': ''}), choices=STATUS_CHOICES)

    def save():
        job = self.cleaned_data['job']
        status = self.cleaned_data['status']
        ctj = CandidateToJob(
            job = job,
            status = status,
            candidate = candidate,
        )
        ctj.save()

        return ctj


--
You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/Ve_RLvkK4Gs/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.

Ronaldo Bahia

unread,
Apr 6, 2015, 3:56:14 PM4/6/15
to django...@googlegroups.com, vkhe...@gmail.com
SOLVED.

It turns out a better approach was to call objects in the template like:

<form role="form" method="post" action="{{ action }}">
{% csrf_token %}
<div class="form-group">
<label for="id_job">Select Job Position</label>
<select class="form-control" id="id_job" name="job">
<option value="">Choose</option>
{% for job in user.company.job_set.all %}
<option value="{{ job.title }}">{{ job.title }}</option>
{% endfor %}
</select>
</div>
<div class="form-group">
<label for="id_status">Status</label>
{{ form.status }}
{{ form.status.errors }}
</div>
<button type="submit" class="btn btn-sm btn-success">Confirm</button>
</form>

class SendCandForm(forms.Form):

job = forms.CharField(label="Vaga", required=True, widget=forms.TextInput(attrs={'class': 'form-control'}))

STATUS_CHOICES = (
('0', 'Novo'),
('1', 'Não aprovado'),
('2', 'Pré Selecionado'),
('3', 'Contratado')
)
status = forms.ChoiceField(required=True, widget=forms.RadioSelect(attrs={'class': ''}), choices=STATUS_CHOICES)
def save():
job = self.cleaned_data['job']
status = self.cleaned_data['status']
ctj = CandidateToJob(
job = job,
status = status,
candidate = candidate,
)
ctj.save()

return ctj
To unsubscribe from this group and all its topics, send an email to django-users+unsubscribe@googlegroups.com.

To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.

Josué García Yza

unread,
Apr 10, 2015, 1:49:11 AM4/10/15
to django...@googlegroups.com, vkhe...@gmail.com
you can ovewrite choices in __init__ method of your form class

def __init__(self, *args, **kwargs):
    super(urClass, self).__init__(*args, **kwargs)
    self.fields['your_field'].choices = [('', 'Escolher')] + [(id, id) for id in job]


change the list knowing which user is logged and filtering


this way you dont have to deal with it on the template.

Ronaldo Bahia

unread,
Apr 10, 2015, 2:03:26 PM4/10/15
to django...@googlegroups.com, Vijay Khemlani
Thanks a lot.


--
You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/Ve_RLvkK4Gs/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-users...@googlegroups.com.

To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
Reply all
Reply to author
Forward
0 new messages