Is it possible to concatenate QuerySets?

2024/11/14 23:36:19

After a search of a database I end up with an array of querysets. I wanted to concatenate these queryset somewhat like we can do with list elements. Is this possible or maybe there an altogether better way to do this? The end goal here is to get queryset for rows of table that contain one of a set of strings in a field.

for i in range(0,(searchDiff+1)):filterString = str(int(searchRange[0]) + i)myQuerySetTwoD.append(my.objects.filter(asn=filterString))for j in range(0,(len(myQuerySetTwoD)-1)):myQuerySet = myQuerySet + myQuerySetTwoD[j]

UPDATE: Found my own answer (something about writing the question down maybe)

Just

from itertools import chain

then replace

myQuerySet = myQuerySet + myQuerySetTwoD[j]

with

BgpAsnList = chain(BgpAsnList,BgpAsnListTwoD[j])
Answer

I think the proper way to do that is to use the | operator (that is, if the QuerySets are of the same type):

qset = MyModel.objects.none()
for filterString in list_of_filterStrings:qset_temp = MyModel.objects.filter(asn=filterString)qset = qset | qset_temp
https://en.xdnf.cn/q/71497.html

Related Q&A

Pickling a Python Extension type defined as a C struct having PyObject* members

I am running C++ code via Python and would like to pickle an extension type.So I have a C++ struct (py_db_manager) containing pointers to a database object and a object manager object (both written in …

Generating random ID from list - jinja

I am trying to generate a random ID from a list of contacts (in Python, with jinja2) to display in an HTML template. So I have a list of contacts, and for the moment I display all of them in a few cell…

Unit testing Flask app running under uwsgi

I’m relatively new to python and am looking for a pythonic way to handle this practice. I’ve inherited a fairly trivial Python 2.7 Flask app that runs under uwsgi that I want to add some unit tests t…

fastest way to find the smallest positive real root of quartic polynomial 4 degree in python

[What I want] is to find the only one smallest positive real root of quartic function ax^4 + bx^3 + cx^2 + dx + e [Existing Method] My equation is for collision prediction, the maximum degree is quarti…

Split strings by 2nd space

Input :"The boy is running on the train"Output expected:["The boy", "boy is", "is running", "running on", "on the", "the train"]Wha…

Searching for a random python program generator

Im searching for a program that can generate random but valid python programs, similar to theRandom C program generator. I was trying to do this myself giving random input to the python tokenize.untoke…

Python tk framework

I have python code that generates the following error:objc[36554]: Class TKApplication is implemented in both /Library/Frameworks/Tk.framework/Versions/8.5/Tk and /System/Library/Frameworks/Tk.framewor…

SQLAlchemy relationship on many-to-many association table

I am trying to build a relationship to another many-to-many relationship, the code looks like this: from sqlalchemy import Column, Integer, ForeignKey, Table, ForeignKeyConstraint, create_engine from …

Python: interpolating in a triangular mesh

Is there any decent Pythonic way to interpolate in a triangular mesh, or would I need to implement that myself? That is to say, given a (X,Y) point well call P, and a mesh (vertices at (X,Y) with val…

Customizing pytest junitxml failure reports

I am trying to introspect test failures and include additional data into the junit xml test report. Specifically, this is a suite of functional tests on an external product, and I want to include the p…