我建立的詞彙,並有以下型號:瞭解Django的GenericForeignKey和GenericRelation
class Word(Model):
name = CharField(max_length=75)
class EnNoun(Model):
word = OneToOneField(Word)
class FrNoun(Model):
word = ForeignKey(Word)
gender = CharField()
同一個詞可以在兩個EnNoun
和FrNoun
。 是否可以使用最少的查詢次數(對於語言和詞類將會有更多類似的類,如ItAdverb
),爲給定詞提取結果EnNoun
和FrNoun
?
如何從一個lang存儲翻譯到另一個(查詢20+表不是一個選項)。
是GenericForeign
鑰匙有什麼用? 我如何一般使用它們?
謝謝。
UPDATE:
有如下翻譯類:
@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。
確定。謝謝。我認爲在這種情況下GenericForeignKey效率不高。但我現在在做翻譯表。我認爲這是事實。查看更新 –
當你收到這個'to_word'錯誤?似乎你正在嘗試做某種過濾?如果你只爲'from_word'和'to_word'屬性使用'Word'類,那麼你可以安全地使用普通的'ForeignKey'而不是'GenericForeignKey'。 – Todor
我用get_or_create。如何對通用密鑰進行預過濾? –