I am trying to parse this Django code (without any familiarity with Django, I 开发者_StackOverflow社区might add)
Question.objects.filter(children__marked=True)
I know the model contains a table called "forum_node" with one of the column named "marked". From what I understand, this statement fetches all the questions where any of its children (= answers) are accepted (or "marked"). How does this magic work?
To understand what happens you should take a closer look at Django's docs explaining the query options and the object-relational mapping.
children
refers to a related model (there should be an according m2m or foreign key field named children on your question
model, indicating the related model, e.g. node
) and marked
is a field on the related model.
The Node
model probably contains something like:
question = models.ForeignKey(Question, related_name='children')
marked = models.BooleanField()
The statement:
Question.objects.filter(children__marked=True)
Is just doing a join on the two tables and returning Question
models that have at least one Node
model with marked=True
.
精彩评论