This repository was archived by the owner on May 25, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
add docs for QSet filters #98
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,122 @@ | ||
| ## Introduction | ||
| Once you’ve created your data models, **Supadantic** automatically gives you a database-abstraction API that lets you create, retrieve, update and delete objects with filters. | ||
|
|
||
| Throughout this guide we’ll refer to the following models: | ||
| ```python | ||
| from supadantic.models import BaseSBModel | ||
|
|
||
| class Author(BaseSBModel): | ||
| name: str | ||
| surname: str | ||
| age: int | ||
| nationality: str | ||
|
|
||
|
|
||
| class Book(BaseSBModel): | ||
| name: str | ||
| genre: str | ||
| count_pages: int | None = None | ||
| author_id: int | ||
| ``` | ||
|
|
||
| And the following data for: | ||
|
|
||
| **Author** | ||
|  | ||
| **Book** | ||
|  | ||
|
|
||
| If you don't know how to create the data via **supadantic**, check it out [here](https://makridenko.github.io/supadantic/models/base_sb_model/?h=save#supadantic.models.BaseSBModel.save). | ||
|
|
||
| ## Retrieving objects | ||
| To retrieve objects from your database, construct a [QuerySet](https://makridenko.github.io/supadantic/q_set/) on your model class. | ||
|
|
||
| A [QuerySet](https://makridenko.github.io/supadantic/q_set/) represents a collection of objects from your database. **It can have zero, one or many filters**. | ||
|
|
||
| ### Retrieving all objects | ||
| ```python | ||
| authors = Author.objects.all() | ||
| ``` | ||
|
|
||
| ### Retrieving a single object with `get()` method | ||
| ```python | ||
| jack_london = Author.objects.get(name="Jack London") | ||
| ``` | ||
| >**Pay attention💡** | ||
| [get()](https://makridenko.github.io/supadantic/q_set/#supadantic.q_set.QSet.get) should return **one object**, otherwise it will raise `DoesNotExist` or `MultipleObjectsReturned` exceptions. | ||
|
|
||
| ### Retrieving specific objects with `filter()` and `exclude()` methods | ||
| If you want to select only a subset of the complete set of objects, you refine the initial [QuerySet](https://makridenko.github.io/supadantic/q_set/), adding filter conditions. The two most common ways to refine a [QuerySet](https://makridenko.github.io/supadantic/q_set/) are: | ||
|
|
||
| - **filter(\*\*kwargs)** — returns a new [QuerySet](https://makridenko.github.io/supadantic/q_set/) containing objects that **match the given lookup parameters**. | ||
| - **exclude(\*\*kwargs)** — returns a new [QuerySet](https://makridenko.github.io/supadantic/q_set/) containing objects that **do not match the given lookup parameters**. | ||
|
|
||
| The lookup parameters (**\*\*kwargs** in the above function definitions) should be in the following format `field__lookuptype=value` (**double-underscore**), for example: | ||
| ```python | ||
| authors = Author.objects.filter(age__gt=50) | ||
| ``` | ||
|
|
||
| #### `Equal` and `Not Equal` filters | ||
| ```python | ||
| books = Book.objects.filter(name="The Idiot") | ||
| ``` | ||
| > Note that the **non-equality filter** is implemented through the `exclude` method. | ||
|
|
||
| ```python | ||
| books = Book.objects.exclude(name="The Idiot") | ||
| ``` | ||
|
|
||
| #### `Greater Than` and `Greater Than Or Equal` filters | ||
| ```python | ||
| books = Book.objects.filter(count_pages__gt=600) # 600 is not included | ||
|
|
||
| books = Book.objects.filter(count_pages__gte=600) # 600 is included | ||
| ``` | ||
|
|
||
| `Exclude` method will be the opposite of this methods: | ||
| ```python | ||
| books = Book.objects.exclude(count_pages__gt=600) # it will have the logic of lte (less than or equal) filter | ||
|
|
||
| books = Book.objects.exclude(count_pages__gte=600) # it will have the logic of lt (less than) filter | ||
| ``` | ||
|
|
||
| #### `Less Than` and `Less Than Or Equal` filters | ||
| ```python | ||
| authors = Author.objects.filter(id__lt=6) # 6 is not included | ||
|
|
||
| authors = Author.objects.filter(id__lte=6) # 6 is included | ||
| ``` | ||
|
|
||
| `Exclude` method will be the opposite of this methods: | ||
| ```python | ||
| authors = Author.objects.exclude(id__lt=6) # it will have the logic of gte (greater than or equal) filter | ||
|
|
||
| authors = Author.objects.exclude(id__lte=6) # it will have the logic of gt (greater than) filter | ||
| ``` | ||
|
|
||
| #### `Include` filter | ||
| ```python | ||
| books = User.objects.filter(genre__in=["Novel", "Poems"]) | ||
|
makridenko marked this conversation as resolved.
|
||
| ``` | ||
| >**Pay attention💡** | ||
| `__in` filter supports any `Iterable` object e.g. `list`, `tuple`, `set`. | ||
|
|
||
| ### Chaining filters | ||
| The result of refining a [QuerySet](https://makridenko.github.io/supadantic/q_set/) is itself a [QuerySet](https://makridenko.github.io/supadantic/q_set/), so it’s possible to chain refinements together. For example: | ||
| ```python | ||
| books = ( | ||
| Book.objects | ||
| .filter(genre="Novel") | ||
| .filter(count_pages__gt=600) | ||
| .filter(author_id=5) | ||
| ) | ||
|
Comment on lines
+106
to
+112
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. let's add to example that you can combine |
||
| ``` | ||
| We also can combine `filter()` and `exclude()` methods: | ||
| ```python | ||
| books = ( | ||
| Book.objects | ||
| .filter(genre="Novel") | ||
| .filter(count_pages__gt=600) | ||
| .exclude(author_id__lt=2) | ||
| ) | ||
| ``` | ||
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.