Python 如何将模型实例添加到 django 查询集中?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29587382/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to add an model instance to a django queryset?
提问by June
It seems like a django queryset behaves somehow like a python list.
看起来 django 查询集的行为有点像 python 列表。
But it doesn't support list's .append() method as I know.
但据我所知,它不支持列表的 .append() 方法。
What I want to do is like:
我想做的是:
from my_django_app.models import MyModel
queryset = MyModel.objects.none()
queryset.append(MyModel.objects.first()) ## no list's .append() method!
Is there any way to add an model instance to an existing queryset?
有没有办法将模型实例添加到现有的查询集中?
采纳答案by Feuermurmel
You can also use the |
operator to create a union:
您还可以使用|
运算符来创建联合:
queryset = MyModel.objects.none()
instance = MyModel.objects.first()
queryset |= MyModel.objects.filter(pk=instance.pk)
But be warned that this will generate different queries depending on the number of items you append this way, making caching of compiled queries inefficient.
但请注意,这将根据您以这种方式附加的项目数量生成不同的查询,从而使已编译查询的缓存效率低下。
回答by itzMEonTV
Queryset is not a list
查询集不是 list
So
所以
to_list = queryset.values()
To combine queryset
结合 queryset
from itertools import chain
result_queryset = list(chain(queryset1, queryset2))
or
或者
querysets = [queryset1, queryset2]
result_queryset = list(chain(*querysets))
回答by Daniel Roseman
No. A queryset is a representation of a query- hence the name - not an arbitrary collection of instances.
不。查询集是查询的表示- 因此得名 - 不是实例的任意集合。
If you really need an actual queryset rather than a list, you could try accumulating the IDs of the objects you need and then getting the objects via an __in
query:
如果你真的需要一个实际的查询集而不是一个列表,你可以尝试累积你需要的对象的 ID,然后通过__in
查询获取对象:
list_of_ids = []
list_of_ids.append(my_id)
...
queryset = MyModel.objects.filter(id__in=list_of_ids)
This isn't very efficient, though.
不过,这不是很有效。
回答by SuperNova
This can be done using union
. After doing this, the type of the result can be seen as <class 'django.db.models.query.QuerySet'>
. So two querysets can be combined. Lets see an example.
这可以使用union
. 这样做之后,结果的类型可以看作是<class 'django.db.models.query.QuerySet'>
. 因此可以组合两个查询集。让我们看一个例子。
query1 = User.objects.filter(is_active=True)
query2 = User.objects.filter(is_active=False)
combined_query = query1.union(query2)
print (type(combined_query))
The above program will print result as below, confirming it is a queryset
上面的程序将打印结果如下,确认它是一个查询集
<class 'django.db.models.query.QuerySet'>
So basically Django executes the below query for union.
所以基本上 Django 执行下面的联合查询。
(SELECT "auth_user"."id", "auth_user"."password", "auth_user"."last_login", "auth_user"."is_superuser", "auth_user"."username", "auth_user"."first_name", "auth_user"."last_name", "auth_user"."email", "auth_user"."is_staff", "auth_user"."is_active", "auth_user"."date_joined" FROM "auth_user" WHERE "auth_user"."is_active" = True)
UNION
(SELECT "auth_user"."id", "auth_user"."password", "auth_user"."last_login", "auth_user"."is_superuser", "auth_user"."username", "auth_user"."first_name", "auth_user"."last_name", "auth_user"."email", "auth_user"."is_staff", "auth_user"."is_active", "auth_user"."date_joined" FROM "auth_user" WHERE "auth_user"."is_active" = False)
This also means that there will be error(django.db.utils.ProgrammingError: each UNION query must have the same number of columns
) if union is tried with two different tables.
这也意味着django.db.utils.ProgrammingError: each UNION query must have the same number of columns
如果对两个不同的表进行联合尝试,将会出现 error( )。