question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Expose pypika query building

See original GitHub issue

Is your feature request related to a problem? Please describe. Tortoise is great, but it lacks some features like jsonb filtering.

Describe the solution you’d like Expose internal make_query, so one can create queryset using existing Tortoise API and update it with more complex logic using query builder. For example

qs = Model.filter(...).limit(100)
raw_query = qs.query  # in current implementation qs.query is empty
raw_query.select().where()

For now one should use

qs = Model.filter(...).limit(100)
qs._make_query()  # make query using pypika as query builder
raw_query = qs.query

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:9
  • Comments:9 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
long2icecommented, May 11, 2021

Now you can use queryset.as_query()

1reaction
AlwxSincommented, Aug 6, 2020

For now, I have to use this snippet

import typing as tp
from copy import copy

from pypika import Criterion
from tortoise import QuerySet, Model


class CriterionQuerySet(QuerySet):
    __slots__ = QuerySet.__slots__ + ("_additional_and_criterions",)

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self._additional_and_criterions: tp.List[Criterion] = []

    @classmethod
    def from_model(cls, model: tp.Type[Model]) -> "CriterionQuerySet":
        """"""
        return cls(model)

    def add_and_criterion(self, criterion: Criterion) -> None:
        self._additional_and_criterions.append(criterion)

    def _clone(self):
        """We need to copy all method because Tortoise use Specific class"""
        # here can be queryset = self.__class__.__new__(self.__class__)
        queryset = CriterionQuerySet.__new__(CriterionQuerySet)
        queryset.fields = self.fields
        queryset.model = self.model
        queryset.query = self.query
        queryset.capabilities = self.capabilities
        queryset._prefetch_map = copy(self._prefetch_map)
        queryset._prefetch_queries = copy(self._prefetch_queries)
        queryset._single = self._single
        queryset._raise_does_not_exist = self._raise_does_not_exist
        queryset._db = self._db
        queryset._limit = self._limit
        queryset._offset = self._offset
        queryset._fields_for_select = self._fields_for_select
        queryset._filter_kwargs = copy(self._filter_kwargs)
        queryset._orderings = copy(self._orderings)
        queryset._joined_tables = copy(self._joined_tables)
        queryset._q_objects = copy(self._q_objects)
        queryset._distinct = self._distinct
        queryset._annotations = copy(self._annotations)
        queryset._having = copy(self._having)
        queryset._custom_filters = copy(self._custom_filters)
        queryset._group_bys = copy(self._group_bys)
        queryset._additional_and_criterions = self._additional_and_criterions
        return queryset

    def _make_query(self) -> None:
        super()._make_query()
        for criterion in self._additional_and_criterions:
            self.query._wheres &= criterion
Read more comments on GitHub >

github_iconTop Results From Across the Web

kayak/pypika - Python Query Builder
PyPika is a python SQL query builder that exposes the full richness of the SQL language using a syntax that reflects the resulting...
Read more >
PyPika - Python Query Builder — PyPika 0.35.16 documentation
PyPika is a Python API for building SQL queries. ... Designed with data analysis in mind, PyPika leverages the builder design pattern to...
Read more >
Advanced Query Features — PyPika 0.35.16 documentation
The package pypika.analytic contains analytic function wrappers. These can be used in SELECT clauses when building queries for databases that support them.
Read more >
pypika Documentation
PyPika is a Python API for building SQL queries. ... Functions which use window aggregation expose the functions rows() and range() with ...
Read more >
Tutorial — PyPika 0.35.16 documentation - Read the Docs
In simple queries like the above example, columns in the “from” table can be referenced by passing string names into the select query...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found