Django lookup table query
I'm quite the Django newb and having a hard time figuring this query out..
Models:
class JakAlgAlgorithms(models.Model):
alg_id = models.AutoField(primary_key=True)
alg_name = models.CharField(max_length=100, blank=True)
alg_description = models.CharField(max_length=1000, blank=True)
def __unicode__(self):
return self.alg_name
class Meta:
db_table = u'jak_alg_algorithms'
class JakAlgXref(models.Model):
xref_alg = models.ForeignKey(JakAlgAlgorithms, related_name='jakalgxref_alg' ,null=True, blank=True)
xref_parent = models.ForeignKey(JakAlgAlgorithms, related_name='jakalgxref_parent', null=True, blank=True)
class Meta:
db_table = u'jak_alg_xref'
What I'm trying to do is return all the parents or children of an algorithm. This is where I've managed to get to..
parents = JakAlgAlgo开发者_运维问答rithms.objects.filter(jakalgxref_parent= algorithm.jakalgxref_alg.all())
However, I've realised that this won't work for multiple parents. How can I have filter take jakalgxref_parent equal to each value returned by algorithim.jakalgxref_alg.all()? Seems like I'm missing something simple :/
Posting my previous comment as an answer for future users.
What you're looking for is this:
parents = JakAlgAlgorithms.objects.filter(jakalgxref_parent__in= algorithm.jakalgxref_alg.all())
Notice I've only added the __in
clause to you filter..
精彩评论