Reputation: 2473
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'))
Upvotes: 3
Views: 2821
Reputation: 21317
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.
Upvotes: 1