Django过滤特定的QuerySet.values() [英] Django Filtering Specific QuerySet.values()

查看:147
本文介绍了Django过滤特定的QuerySet.values()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

比方说,我有一个班级父母和班级 Child 。并且child有一个名为 status 的字段,并且与 Parent ForeignKey 关系。 c>。

Let's say I have a model Class Parent and a Class Child. And child has a field called status and a ForeignKey relationship to Parent.

比方说,我通过调用 p = Parent.objects.filter来调用filter(以便有一个QuerySet)来检索一个父对象。 (pk = 1)

现在,如果我呼叫 p.values('children__name')我将收到该父母的孩子名字的字典列表。

Now if I call p.values('children__name') I will receive a list of dictionaries of the children names to that parent.

我的问题是,如果我想调用 p.values ('children__name'),但仅在孩子的状态是特定的情况下才限制值,我该怎么做?

My question is, if I wanted to call p.values('children__name') but limit the values only if the status of the child was specific, how would I do that?

我还想确保原始QuerySet保持不变,因为我不想将其过滤掉(对于较大的QuerySet)。我只是想过滤基于某些参数的值。

I also want to make sure the original QuerySet is unaltered, as I don't want to filter it down (for larger QuerySets). I just want to filter the values that are based on some parameter.

在Django中有什么方法可以做到这一点?

Is there any way to do this in Django?

推荐答案

您只需过滤:

p.filter(children__status='whatever').values('children__name')

这篇关于Django过滤特定的QuerySet.values()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆