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.

The Python-style is_null() filter does not exist

See original GitHub issue

Describe the bug

The documentation states: https://github.com/collerek/ormar/blob/master/docs/queries/filter-and-sort.md#python-style-filters

isnull: not null can be written as Track.album.name.is_null(False)

But this does not actually seem to work.

To Reproduce

$ python --version
Python 3.9.1

$  pip freeze
aiosqlite==0.17.0
click==7.1.2
databases==0.4.1
Faker==8.1.0
fastapi==0.63.0
fastapi-pagination==0.7.0
h11==0.12.0
ormar==0.10.4
pydantic==1.8
python-dateutil==2.8.1
six==1.15.0
SQLAlchemy==1.3.23
starlette==0.13.6
text-unidecode==1.3
typing-extensions==3.7.4.3
uvicorn==0.13.4

Try to run this script with uvicorn main:app:

from typing import List, Optional

import databases
import sqlalchemy
from fastapi import FastAPI

import ormar

app = FastAPI()
metadata = sqlalchemy.MetaData()
database = databases.Database("sqlite:///test.db")
app.state.database = database


class Item(ormar.Model):
    class Meta:
        tablename = "items"
        metadata = metadata
        database = database

    id: int = ormar.Integer(primary_key=True)
    name: Optional[str] = ormar.String(nullable=True, max_length=100)


@app.on_event("startup")
async def startup() -> None:
    engine = sqlalchemy.create_engine(str(database.url))
    metadata.drop_all(engine)
    metadata.create_all(engine)

    await database.connect()

    for _ in range(100):
        await Item.objects.create(name="Name")


@app.on_event("shutdown")
async def shutdown() -> None:
    database_ = app.state.database
    if database_.is_connected:
        await database_.disconnect()


@app.get("/items/", response_model=List[Item])
async def get_items():
    items = await Item.objects.filter(Item.name.is_null(False)).all()
    return items

Error when run:

$ uvicorn main3:app --reload
INFO:     Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
INFO:     Started reloader process [63484] using statreload
INFO:     Started server process [63487]
INFO:     Waiting for application startup.
INFO:     Application startup complete.
INFO:     127.0.0.1:60078 - "GET /docs HTTP/1.1" 200 OK
INFO:     127.0.0.1:60078 - "GET /openapi.json HTTP/1.1" 200 OK
INFO:     127.0.0.1:60081 - "GET /items/ HTTP/1.1" 500 Internal Server Error
ERROR:    Exception in ASGI application
Traceback (most recent call last):
  File "./venv/lib/python3.9/site-packages/uvicorn/protocols/http/h11_impl.py", line 396, in run_asgi
    result = await app(self.scope, self.receive, self.send)
  File "./venv/lib/python3.9/site-packages/uvicorn/middleware/proxy_headers.py", line 45, in __call__
    return await self.app(scope, receive, send)
  File "./venv/lib/python3.9/site-packages/fastapi/applications.py", line 199, in __call__
    await super().__call__(scope, receive, send)
  File "./venv/lib/python3.9/site-packages/starlette/applications.py", line 111, in __call__
    await self.middleware_stack(scope, receive, send)
  File "./venv/lib/python3.9/site-packages/starlette/middleware/errors.py", line 181, in __call__
    raise exc from None
  File "./venv/lib/python3.9/site-packages/starlette/middleware/errors.py", line 159, in __call__
    await self.app(scope, receive, _send)
  File "./venv/lib/python3.9/site-packages/starlette/exceptions.py", line 82, in __call__
    raise exc from None
  File "./venv/lib/python3.9/site-packages/starlette/exceptions.py", line 71, in __call__
    await self.app(scope, receive, sender)
  File "./venv/lib/python3.9/site-packages/starlette/routing.py", line 566, in __call__
    await route.handle(scope, receive, send)
  File "./venv/lib/python3.9/site-packages/starlette/routing.py", line 227, in handle
    await self.app(scope, receive, send)
  File "./venv/lib/python3.9/site-packages/starlette/routing.py", line 41, in app
    response = await func(request)
  File "./venv/lib/python3.9/site-packages/fastapi/routing.py", line 201, in app
    raw_response = await run_endpoint_function(
  File "./venv/lib/python3.9/site-packages/fastapi/routing.py", line 148, in run_endpoint_function
    return await dependant.call(**values)
  File "./main3.py", line 46, in get_items
    items = await Item.objects.filter(Item.name .is_null(False)).all()
  File "./venv/lib/python3.9/site-packages/ormar/queryset/field_accessor.py", line 46, in __getattr__
    return object.__getattribute__(self, item)  # pragma: no cover
AttributeError: 'FieldAccessor' object has no attribute 'is_null'

Expected behavior

Query to complete successfully and filter out Items with null name.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:7 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
collerekcommented, Apr 22, 2021

Thanks, that means a lot 😃 Try to build a community around ormar to get more people to test/contribute but it’s going super slow 😃 Anyway, docs typo fixed.

1reaction
collerekcommented, Apr 22, 2021

Yeah there is a problem with dynamic attributes typing and autocompletion in ormar, there is a plugin for pydantic (https://github.com/koxudaxi/pydantic-pycharm-plugin), and I am thinking about forking it and creating one for ormar that hacks around autocompletion, but since I don’t know kotlin and didn’t write java in years (not to mention never created pycharm plugin) it’s gonna take a while 😁

Read more comments on GitHub >

github_iconTop Results From Across the Web

Unable to filter NaT using isnull() - python - Stack Overflow
I have dataframe where one column has significant no. of Nat. I'm using isnull() to filter them but it does not seem to...
Read more >
python pandas filter operations and operator or isnull not is null
It can be observed that there are two records where the Open values are null as shown in figure 6. Figure 6: Using...
Read more >
Filtering and sorting data - ormar
Python style filters ... So operations like filter() , select_related() , limit() and offset() etc. can be chained. ... Note that you do...
Read more >
Python Pandas filter operations and operator or isnull not is null
In this video we will see how to apply filter conditions on multiple columns using AND , OR operators.Also we will see how...
Read more >
PySpark isNull() & isNotNull() - Spark by {Examples}
isNotNull – PySpark isNotNull() method returns True if the current expression is NOT NULL/None. This function is only present in the Column class...
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