【发布时间】:2016-10-20 07:57:24
【问题描述】:
我正在构建词汇并拥有以下模型:
class Word(Model):
name = CharField(max_length=75)
class EnNoun(Model):
word = OneToOneField(Word)
class FrNoun(Model):
word = ForeignKey(Word)
gender = CharField()
EnNoun 和 FrNoun 中都可以出现相同的单词。是否可以使用最少数量的查询为EnNoun 和FrNoun 获取给定单词的结果(语言和词性会有更多类似的类,例如ItAdverb)?
如何存储从一种语言到另一种语言的翻译(不能查询 20 多个表)。
GenericForeign 键有用吗?我一般如何使用它们?
我有以下翻译课:
@python_2_unicode_compatible
class Translation(Model):
from_content_type = ForeignKey(ContentType, related_name='from_word_content_type')
from_object_id = UUIDField(default=uuid.uuid4)
from_word = GenericForeignKey('from_content_type', 'from_object_id')
to_content_type = ForeignKey(ContentType, related_name='to_word_content_type')
to_object_id = UUIDField(default=uuid.uuid4)
to_word = GenericForeignKey('to_content_type', 'to_object_id')
但它不起作用:
字段“to_word”不会生成自动反向关系,因此不能用于反向查询。如果是 GenericForeignKey,请考虑添加 GenericRelation。
【问题讨论】:
标签: django django-models