1

Django 菜鸟在这里。我正在构建字典/词典并希望将单词同义词相互链接。

模型.py

class Word(models.Model):
    word = models.Charfield()

class Synonym(models.Model):
    word_from = models.ForeignKey(Word, blank=True, null=True, related_name="word_from")
    word_to = models.ForeignKey(Word, blank=True, null=True, related_name="word_to")

你如何获得相关的同义词?我正在使用generic.DetailView。这也是建立单词之间联系的正确方法吗?

提前致谢!

4

2 回答 2

0

Assuming a word could have several synonyms and every synonym could also be synonymous to other words, you can use Django's ManyToManyField, referencing self.

Here's such an example:

class Word(models.Model):
    word = models.CharField(...)
    synonyms = models.ManyToManyField("self")
于 2013-02-10T23:08:14.833 回答
0

我不明白word_fromand的目的是什么word_to。但是最简单的单词和同义词模型可以通过一张表来实现:

class Word(models.Model):
    word = models.Charfield()
    synonym = models.ForeignKey(
        'self', null=True, blank=True,
        related_name='synonyms')
于 2013-02-10T22:22:23.940 回答