2010-05-11 170 views
0

这是我想要涉及的模型。我希望收集以出现的形式出现。Django ForeignKey TemplateSyntaxError和ProgrammingError

class Collection(models.Model): 
    id = models.AutoField(primary_key=True, null=True) 
    code = models.CharField(max_length=100, null=True, blank=True) 
    address = models.CharField(max_length=100, null=True, blank=True) 
    collection_name = models.CharField(max_length=100) 

    def __unicode__(self): 
     return self.collection_name 

    class Meta: 
     db_table = u'collection' 
     ordering = ('collection_name',) 

class Occurrence(models.Model): 
    id = models.AutoField(primary_key=True, null=True) 
    reference = models.IntegerField(null=True, blank=True, editable=False) 
    collection = models.ForeignKey(Collection, null=True, blank=True, unique=True), 
    modified = models.DateTimeField(null=True, blank=True, auto_now=True) 
    class Meta: 
     db_table = u'occurrence' 

我每次去检查发生对象

TemplateSyntaxError at /admin/hotiapp/occurrence/ 
Caught an exception while rendering: column occurrence.collection_id does not exist 
LINE 1: ...LECT "occurrence"."id", "occurrence"."reference", "occurrenc.. 

而每一次我得到这个错误我尝试添加一个新的发生对象我得到这个错误

ProgrammingError at /admin/hotiapp/occurrence/add/ 
column occurrence.collection_id does not exist 
LINE 1: SELECT (1) AS "a" FROM "occurrence" WHERE "occurrence"."coll... 

什么我做错了吗?或者ForeignKey如何工作?

回答

1

问题是,自添加ForeignKey以来,您尚未更新数据库表定义。正如文档中明确指出的那样,syncdb不会为你做这件事。您需要手动更新SQL,或使用像South这样的工具。

+0

是的,这是问题。谢谢 – 2010-05-11 19:43:02