我的 django 应用程序的 models.py 中有这些类。我使用 postgresql 8.3 作为数据库
class Course(models.Model):
students = models.ManyToManyField(User)
title = models.CharField(max_length=200)
description = models.TextField()
def __unicode__(self):
return self.title
class Meta:
verbose_name_plural="Courses"
class CourseChoicesForm(forms.Form):
courseoption = forms.ChoiceField(choices=[(x.id,x.title) for x in Course.objects.all()])
当我运行时python manage.py syncdb
,我收到一个错误,上面写着relation course is not found
...
File "/home/me/Django-1.4/django/utils/importlib.py", line 35, in import_module
__import__(name)
File "/home/me/dev/python/django/myprj/myapp/models.py", line 50, in <module>
class CourseChoicesForm(forms.Form):
File "/home/me/dev/python/django/myprj/myapp/models.py", line 52, in CourseChoicesForm
courseoption = forms.ChoiceField(choices=[(x.id,x.title) for x in Course.objects.all()])
...
File "/home/me/Django-1.4/django/db/backends/postgresql_psycopg2/base.py", line 52, in execute
return self.cursor.execute(query, args)
django.db.utils.DatabaseError: relation "myapp_course" does not exist
更新: 我解决了这个问题如下
class CourseChoicesForm(forms.Form):
courseoption = forms.ChoiceField(choices=[],required=False)
def __init__(self, *args, **kwargs):
super(CourseChoicesForm, self).__init__(*args, **kwargs)
self.fields['courseoption'].choices = [(x.id,x.title) for x in Course.objects.all()]
不过,我不太清楚为什么会发生这种行为。有人可以解释一下吗?