Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'ModelChoiceField' object has no attribute 'objects'

I am using django framework for the first time. I want to fetch the data from my model course to show it in choice field of form. but when i am using the same model for two diiferent field in single form, it is showing the error 'ModelChoiceField' object has no attribute 'objects'. here is my code.

models.py:

from django.db import models
class course(models.Model):
    course_id = models.CharField(primary_key = True, max_length = 2)
    course_name = models.CharField(max_length = 20)
    stream = models.CharField(max_length = 15)
    number_of_sem = models.IntegerField(max_length = 2)

    def __unicode__(self):
        return self.course_id

forms.py:

from django import forms
from feedback_form.models import course

class loginForm(forms.Form):
    course = forms.ModelChoiceField(queryset=course.objects.values_list('course_name', flat = True))
    semester = forms.ModelChoiceField(queryset=course.objects.values('number_of_sem'))
like image 771
Rohini Choudhary Avatar asked Mar 12 '26 21:03

Rohini Choudhary


1 Answers

Problem is in forms.py

class loginForm(forms.Form):
    course = forms.ModelChoiceField(queryset=course.objects.values_list('course_name', flat = True))
    semester = forms.ModelChoiceField(queryset=course.objects.values('number_of_sem'))

You have course field in forms.py when your refer course in your forms.ModelChoiceField it got confuse about course Model and course field.

Please change field variable name.

like image 131
Nilesh Avatar answered Mar 15 '26 13:03

Nilesh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!