2
運行我在Django的application.I的models.py有這些類正在使用PostgreSQL 8.3作爲數據庫中找不到關係時執行syncdb是在Django
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
UPDATE: 我解決了這個如下
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()]
不過,我不太清楚爲什麼會發生這種行爲。有人請解釋一下嗎?