执行syncdb时,Django unique_together不允许跨应用程序使用ForeignKey字段

发布于 2024-10-06 12:04:13 字数 2032 浏览 0 评论 0原文

我正在尝试对下面列出的 TranslationRequest 模型创建唯一约束。 TranslationRequestMachineTranslator 模型具有外键关系,该模型存在于另一个 Django 应用程序中。当我尝试运行syncdb时,出现错误:错误:一个或多个模型未验证: wt_articles.translationrequest:“unique_together”指的是翻译者,一个不存在的字段。检查您的语法。

当我从 unique_constraint 规范中删除 translator 时,syncdb 可以正确运行。注意:我使用 Sqlite3 作为我的后端数据库。

以下是 TranslationRequestSourceArticle 的定义。

from wt_translation.models import MachineTranslator

class TranslationRequest(models.Model):
    article = models.ForeignKey(SourceArticle)
    target_language = models.ForeignKey(Language, db_index=True)
    date = models.DateTimeField(_('Request Date'))
    translator = models.ForeignKey(MachineTranslator),
    status = models.CharField(_('Request Status'),
                              max_length=32,
                              choices=TRANSLATION_STATUSES)

    class Meta:
        unique_together = ("article", "target_language", "translator")

class SourceArticle(models.Model):
    title = models.CharField(_('Title'), max_length=255)
    language = models.ForeignKey(Language, db_index=True)
    timestamp = models.DateTimeField(_('Import Date'), default=datetime.now())
    doc_id = models.CharField(_('Document ID'), max_length=512)
    source_text = models.TextField(_('Source Text'))
    sentences_processed = models.BooleanField(_('Sentences Processed'))

这是 MachineTranslator 的定义,在另一个(但引用了 Django 应用程序)中。

class MachineTranslator(models.Model):
    shortname = models.CharField(_('Name'), max_length=50)
    supported_languages = models.ManyToManyField(LanguagePair)
    description = models.TextField(_('Description'))
    type = models.CharField(_('Type'), max_length=32, choices=TRANSLATOR_TYPES, default='Serverland'),
    timestamp = models.DateTimeField(_('Refresh Date'), default=datetime.now())
    is_alive = models.BooleanField()

此代码示例中并未包含所有依赖项。感谢您的帮助!

I'm trying to create a unique constraint on my TranslationRequest model listed below. TranslationRequest has a foreign key relationship with a MachineTranslator model, which exists in another Django application. When I try to run syncdb, I get the error: Error: One or more models did not validate:
wt_articles.translationrequest: "unique_together" refers to translator, a field that doesn't exist. Check your syntax.

When I remove translator from the unique_constraint specification, syncdb runs correctly. Note: I'm using Sqlite3 as my back-end database.

Here are the definitions of TranslationRequest and SourceArticle.

from wt_translation.models import MachineTranslator

class TranslationRequest(models.Model):
    article = models.ForeignKey(SourceArticle)
    target_language = models.ForeignKey(Language, db_index=True)
    date = models.DateTimeField(_('Request Date'))
    translator = models.ForeignKey(MachineTranslator),
    status = models.CharField(_('Request Status'),
                              max_length=32,
                              choices=TRANSLATION_STATUSES)

    class Meta:
        unique_together = ("article", "target_language", "translator")

class SourceArticle(models.Model):
    title = models.CharField(_('Title'), max_length=255)
    language = models.ForeignKey(Language, db_index=True)
    timestamp = models.DateTimeField(_('Import Date'), default=datetime.now())
    doc_id = models.CharField(_('Document ID'), max_length=512)
    source_text = models.TextField(_('Source Text'))
    sentences_processed = models.BooleanField(_('Sentences Processed'))

Here is the definition of MachineTranslator, in a different (but referenced Django application).

class MachineTranslator(models.Model):
    shortname = models.CharField(_('Name'), max_length=50)
    supported_languages = models.ManyToManyField(LanguagePair)
    description = models.TextField(_('Description'))
    type = models.CharField(_('Type'), max_length=32, choices=TRANSLATOR_TYPES, default='Serverland'),
    timestamp = models.DateTimeField(_('Refresh Date'), default=datetime.now())
    is_alive = models.BooleanField()

Not all of the dependencies have been included in this code sample. Thanks for your help!

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

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

发布评论

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

评论(1

内心激荡 2024-10-13 12:04:13

我不知道这是否是一个拼写错误,但我在声明翻译器 = models.ForeignKey(MachineTranslator) 的行末尾看到了“,”逗号,

这就是为什么该属性可能不可见的原因

i dont' know if it is a typo but i see s "," comma at the end of the line where you declare your translator = models.ForeignKey(MachineTranslator)

This is why maybe the attribute is ot seens

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