Skip to content

Commit 37d67d7

Browse files
committed
Add contrib module with utilities that have been helpful over the years
1 parent 663ecf6 commit 37d67d7

File tree

6 files changed

+545
-0
lines changed

6 files changed

+545
-0
lines changed

psqlextra/contrib/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# psqlextra.contrib
2+
3+
This module contains a arbitrary collection of utilities and snippets that build on top of core functionality provided by django-postgres-extra.
4+
5+
This collection is UNTESTED, UNSUPPORTED and UNDOCUMENTED. They are only provided here as an inspiration. Use at your own risk.

psqlextra/contrib/__init__.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from .model_data_migrator import PostgresModelDataMigrator
2+
from .static_row import StaticRowQueryCompiler, StaticRowQuerySet
3+
from .transaction import no_transaction
4+
5+
__all__ = [
6+
"PostgresModelDataMigrator",
7+
"PostgresModelDataMigratorState" "StaticRowQuery",
8+
"StaticRowQueryCompiler",
9+
"StaticRowQuerySet",
10+
"no_transaction",
11+
]

psqlextra/contrib/expressions.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
from django.db import models
2+
from django.db.models.expressions import CombinedExpression, Func
3+
4+
5+
class Equals(CombinedExpression):
6+
"""Expression that constructs `{lhs} = {rhs}`.
7+
8+
Used as an alternative to Django's `Q` object when the
9+
left-hand side is a aliased field not known to Django.
10+
"""
11+
12+
connector: str = "="
13+
14+
def __init__(self, lhs, rhs) -> None:
15+
super().__init__(
16+
lhs, self.connector, rhs, output_field=models.BooleanField()
17+
)
18+
19+
20+
class Is(Equals):
21+
"""Expression that constructs `{lhs} IS {rhs}`."""
22+
23+
connector: str = "IS"
24+
25+
26+
class GreaterThen(Equals):
27+
"""Expression that constructs `{lhs} > {rhs}`."""
28+
29+
connector: str = ">"
30+
31+
32+
class LowerThenOrEqual(Equals):
33+
"""Expression that constructs `{lhs} <= {rhs}`."""
34+
35+
connector: str = "<="
36+
37+
38+
class And(Equals):
39+
"""Expression that constructs `{lhs} AND {rhs}`."""
40+
41+
connector: str = "AND"
42+
43+
44+
class Bool(Func):
45+
"""Cast to a boolean."""
46+
47+
function = "BOOL"

0 commit comments

Comments
 (0)