【发布时间】:2010-12-10 21:48:03
【问题描述】:
我正在做一个小词汇测验应用,一个单词的基本模型是这样的:
class Word(models.Model):
id = models.AutoField(primary_key=True)
word = models.CharField(max_length=80)
id_image = models.ForeignKey(Image)
def __unicode__(self):
return self.word
class Meta:
db_table = u'word'
我目前正在测验自己的单词模型是这样的:
class WordToWorkOn(models.Model):
id = models.AutoField(primary_key=True)
id_student = models.ForeignKey(Student)
id_word = models.ForeignKey(Word)
level = models.IntegerField()
def __unicode__(self):
return u'%s %s' % (self.id_word.__unicode__(), self.id_student.__unicode__() )
class Meta:
db_table = u'word_to_work_on'
其中“级别”表示我的学习程度。我已经学过的一组单词有这个模型:
class WordLearned(models.Model):
id = models.AutoField(primary_key=True)
id_word = models.ForeignKey(Word, related_name='word_to_learn')
id_student = models.ForeignKey(Student, related_name='student_learning_word')
def __unicode__(self):
return u'%s %s' % (self.id_word.__unicode__(), self.id_student.__unicode__() )
class Meta:
db_table = u'word_learned'
当 WordToWorkOn 上的查询集返回的结果太少时(因为它们已经学得足够好,可以移入 WordLearned 并从 WordToWorkOn 中删除),我想找一个词添加到它。我不知道一个好的方法是将其限制为 WordLearned 中尚未出现的单词。
所以,一般来说,我想我想对 Words 的查询集执行某种 .exclude() 操作,但它需要根据 WordLearned 表中的成员资格进行排除。有没有好的方法来做到这一点?我找到了很多关于加入查询集的参考资料,但找不到一个很好的关于如何做到这一点的参考资料(可能只是不知道要搜索的正确术语)。
我不想只在每个单词上使用标志来表示已学习、正在使用或未学习,因为最终这将是一个多用户应用程序,我不希望每个用户都有标志.因此,我认为每组有多个表会更好。
感谢所有建议。
【问题讨论】:
标签: django django-models django-queryset