Note that there are some explanatory texts on larger screens.

plurals
  1. POdjango queryset excluding entries in second model
    primarykey
    data
    text
    <p>I'm making a little vocabulary-quiz app, and the basic model for a word is this:</p> <pre><code>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' </code></pre> <p>The model for words I'm currently quizzing myself on is this:</p> <pre><code>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' </code></pre> <p>Where "level" indicates how well I've learned it. The set of words I've already learned has this model:</p> <pre><code>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' </code></pre> <p>When a queryset on WordToWorkOn comes back with too few results (because they have been learned well enough to get moved into WordLearned and deleted from WordToWorkOn), I want to find a Word to add to it. The part I don't know a good way to do is to limit it to Words which are not already in WordLearned.</p> <p>So, generally speaking, I think I want to do an .exclude() of some sort on a queryset of Words, but it needs to exclude based on membership in the WordLearned table. Is there a good way to do this? I find lots of references to joining querysets, but couldn't find a good one on how to do this (probably just don't know the right term to search for).</p> <p>I don't want to just use a flag on each Word to indicate learned, working on it, or not learned, because eventually this will be a multi-user app and I wouldn't want to have flags for every user. Hence, I thought multiple tables for each set would be better.</p> <p>All advice is appreciated.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload