QuerySet
Chainable query builder for repository data access. QuerySets support filtering, excluding, ordering, pagination, and lookup operations.
See Retrieve Aggregates guide for practical usage.
A chainable class to gather a bunch of criteria and preferences (resultset size, order etc.) before execution.
Internally, a QuerySet can be constructed, filtered, sliced, and generally passed around without actually fetching data. No data fetch actually occurs until you do something to evaluate the queryset.
Once evaluated, a QuerySet typically caches its results. If the data in the database
might have changed, you can get updated results for the same query by calling all() on a
previously evaluated QuerySet.
| ATTRIBUTE | DESCRIPTION |
|---|---|
offset |
Number of records after which Results are fetched
|
limit |
The size the recordset to be pulled from database
|
order_by |
The list of parameters to be used for ordering the results.
Use a
|
excludes_ |
Objects with these properties will be excluded from the results
|
filters |
Filter criteria
|
:return Returns a ResultSet object that holds the query results
Initialize either with empty preferences (when invoked on an Entity) or carry forward filters and preferences when chained
Source code in src/protean/core/queryset.py
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 | |
total
property
total
Return the total number of records
items
property
items
Return result values
first
property
first
Return the first result
last
property
last
Return the last result
has_next
property
has_next
Return True if there are more values present
has_prev
property
has_prev
Return True if there are previous values present
filter
filter(*args, **kwargs)
Return a new QuerySet instance with the args ANDed to the existing set.
Source code in src/protean/core/queryset.py
99 100 101 102 103 104 | |
exclude
exclude(*args, **kwargs)
Return a new QuerySet instance with NOT (args) ANDed to the existing set.
Source code in src/protean/core/queryset.py
106 107 108 109 110 111 | |
limit
limit(limit)
Limit number of records
Source code in src/protean/core/queryset.py
151 152 153 154 155 156 157 158 159 | |
offset
offset(offset)
Fetch results after offset value
Source code in src/protean/core/queryset.py
161 162 163 164 165 166 167 168 | |
order_by
order_by(order_by: Union[list, str])
Update order_by setting for filter set
Source code in src/protean/core/queryset.py
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 | |
all
all() -> ResultSet
Primary method to fetch data based on filters
Also trigged when the QuerySet is evaluated by calling one of the following methods
- len()
- bool()
- list()
- Iteration
- Slicing
Source code in src/protean/core/queryset.py
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 | |
update
update(*data, **kwargs)
Updates all objects with details given if they match a set of conditions supplied.
This method updates each object individually, to fire callback methods and ensure validations are run.
Returns the number of objects matched (which may not be equal to the number of objects updated if objects rows already have the new value).
Source code in src/protean/core/queryset.py
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 | |
raw
raw(query: Any, data: Any = None)
Runs raw query directly on the database and returns Entity objects
Note that this method will raise an exception if the returned objects are not of the Entity type.
query is not checked for correctness or validity, and any errors thrown by the plugin or
database are passed as-is. Data passed will be transferred as-is to the plugin.
All other query options like order_by, offset and limit are ignored for this action.
Source code in src/protean/core/queryset.py
294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 | |
delete
delete()
Deletes matching objects from the Repository
Does not throw error if no objects are matched.
Returns the number of objects matched (which may not be equal to the number of objects deleted if objects rows already have the new value).
Source code in src/protean/core/queryset.py
332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 | |
update_all
update_all(*args, **kwargs)
Updates all objects with details given if they match a set of conditions supplied.
This method forwards filters and updates directly to the repository. It does not instantiate entities and it does not trigger Entity callbacks or validations.
Update values can be specified either as a dict, or keyword arguments.
Returns the number of objects matched (which may not be equal to the number of objects updated if objects rows already have the new value).
Source code in src/protean/core/queryset.py
355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 | |
delete_all
delete_all(*args, **kwargs)
Deletes objects that match a set of conditions supplied.
This method forwards filters directly to the repository. It does not instantiate entities and it does not trigger Entity callbacks or validations.
Returns the number of objects matched and deleted.
Source code in src/protean/core/queryset.py
378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 | |
__iter__
__iter__()
Return results on iteration
Source code in src/protean/core/queryset.py
406 407 408 | |
__len__
__len__()
Return length of results
Source code in src/protean/core/queryset.py
410 411 412 | |
__bool__
__bool__()
Return True if query results have items
Source code in src/protean/core/queryset.py
414 415 416 | |
__repr__
__repr__()
Support friendly print of query criteria
Source code in src/protean/core/queryset.py
418 419 420 421 422 423 424 425 426 427 | |
__getitem__
__getitem__(k)
Support slicing of results
Source code in src/protean/core/queryset.py
429 430 431 | |
__contains__
__contains__(k)
Support in operations
Source code in src/protean/core/queryset.py
433 434 435 | |