Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Define the queryset_class in model #150

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions docs/declaring_models.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,13 @@ models = orm.ModelRegistry(database=database)


class Note(orm.Model):
class MyQuerySet(QuerySet):
...

tablename = "notes"
registry = models
# or do not define the queryset_class
queryset_class = MyQuerySet
fields = {
"id": orm.Integer(primary_key=True),
"text": orm.String(max_length=100),
Expand Down
3 changes: 2 additions & 1 deletion orm/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
Text,
Time,
)
from orm.models import Model, ModelRegistry
from orm.models import Model, ModelRegistry, QuerySet

__version__ = "0.3.1"
__all__ = [
Expand Down Expand Up @@ -49,4 +49,5 @@
"UUID",
"Model",
"ModelRegistry",
"QuerySet"
]
7 changes: 6 additions & 1 deletion orm/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ def __new__(cls, name, bases, attrs):
if "tablename" not in attrs:
setattr(model_class, "tablename", name.lower())

model_class.queryset_class = attrs.get("queryset_class")

for name, field in attrs.get("fields", {}).items():
setattr(field, "registry", attrs.get("registry"))
if field.primary_key:
Expand Down Expand Up @@ -485,7 +487,6 @@ def _prepare_order_by(self, order_by: str):


class Model(metaclass=ModelMeta):
objects = QuerySet()

def __init__(self, **kwargs):
if "pk" in kwargs:
Expand All @@ -497,6 +498,10 @@ def __init__(self, **kwargs):
)
setattr(self, key, value)

@property
def objects(self) -> QuerySet:
return self.queryset_class() if self.queryset_class else QuerySet()

@property
def pk(self):
return getattr(self, self.pkname)
Expand Down
29 changes: 29 additions & 0 deletions tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,25 @@ class Product(orm.Model):
}


class Book(orm.Model):
class MyQuerySet(QuerySet):

async def get_or_none(self, **kwargs):
try:
return await super().get(**kwargs)
except NoMatch:
# or raise HttpException(404)
return None

tablename = "products"
registry = models
queryset_class = QuerySet
fields = {
"id": orm.Integer(primary_key=True),
"name": orm.String(max_length=100),
}


@pytest.fixture(autouse=True, scope="function")
async def create_test_database():
await models.create_all()
Expand Down Expand Up @@ -333,3 +352,13 @@ async def test_model_sqlalchemy_filter_operators():
shirt
== await Product.objects.filter(Product.columns.name.contains("Cotton")).get()
)


async def test_queryset_class():
await Book.objects.create(name="book")

b = await Book.objects.get_or_none(name="book")
assert b

b = await Book.objects.get_or_none(name="books")
assert b is None