Django 外键模板语法错误和编程错误

发布于 2024-09-01 20:57:09 字数 1394 浏览 3 评论 0原文

这是我想要关联的模型。我希望集合以发生的形式出现。

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是如何工作的?

This is are my models i want to relate. i want for collection to appear in the form of occurrence.

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'

Every time i go to check the Occurrence object i get this error

 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..

And every time i try to add a new occurrence object i get this error

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

What am i doing wrong? or how does ForeignKey works?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

傲影 2024-09-08 20:57:09

问题是自添加外键以来您尚未更新数据库表定义。正如文档明确指出的那样,syncdb 不会为您执行此操作。您需要手动更新SQL,或者使用South之类的工具。

The problem is that you have not updated your database table definition since adding the ForeignKey. syncdb doesn't do this for you, as the documentation clearly states. You need to update the SQL manually, or use a tool like South.

指尖凝香 2024-09-08 20:57:09

您确定您的意思是

collection = models.ForeignKey(Collection, null=True, blank=True, unique=True),

Nullable 和 Unique 吗?这在某些数据库中可能是不可能的。

一般来说,唯一约束在这里似乎没有多大意义。

您是否想强制建立一对一的关系?使用 OneToOneField。 http://docs.djangoproject.com /en/1.1/ref/models/fields/#django.db.models.OneToOneField

Are you sure you mean

collection = models.ForeignKey(Collection, null=True, blank=True, unique=True),

Nullable and Unique? This may not be possible in some databases.

Generally, the unique constraint doesn't seem to make much sense here.

Are you trying to force a 1-to-1 relationship? Use the OneToOneField. http://docs.djangoproject.com/en/1.1/ref/models/fields/#django.db.models.OneToOneField

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文