2015-01-21 322 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