2015-01-21 313 views
2

我第一次使用django框架。我想从我的模型课程中获取数据,以便在表单的选择区域中显示它。但是当我在单个窗体中为两个不同的字段使用相同的模型时,它显示错误'ModelChoiceField'对象没有属性'对象'。这是我的代码。'ModelChoiceField'对象没有属性'对象'

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')) 
+0

请问stacktrace问题? – Nilesh 2015-01-21 08:52:33

回答

0

问题是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')) 

你有forms.pycourse领域,当你指courseforms.ModelChoiceField它感到困惑course模型和course字段。

请更改字段变量名称。

+0

Thankyou。你的答案解决了我的错误。 – 2015-01-21 09:03:41