Hi - I'm trying to use Tortoise with fastapi / pydantic, porting over some POC code from Sqlalchemy/FastAPI. I have two basic models - Mailbox and Domain. Both reference eachother - i.e. Mailbox belongs to a domain, Domain has many mailboxes. I also have a "*Summary" version of each - i.e. DomainSummary does not have the Mailboxes, and MailboxSummary only has domain_id rather than the relationship.
I use this so that when a user queries a Mailbox, they get Mailbox+DomainSummary, and not a big recursive thing.
I think what I want to do, is use different models for the relationship in different situations. This is easy with Pydantic - but it appears that tortoise requires us to use pydantic_model_creator, which doesn't allow me to specify different pydantic models in certain cases...
This seems like something that would be fairly common, so I must be missing something?
[tool:pytest]
addopts = -n auto --cov=tortoise --tb=native -q
make test
pip install pytest-cov
CategoryResponse
is the pydantic model for responses CategoryResponse = pydantic_model_creator(Categories)
.
Hi, good morning all! Is anyone here looking for a short 1-2 hour paid consulting engagement today? Some background:
I've seen a couple of github threads about doing this, but would love to screen-share and pair with someone who knows what they're doing better than I do. Thanks!
The filters isnull
or not_isnull
are not working for me on foreignkey fieldsqueryset = queryset.filter(parent_category__not_isnull=False)
tortoise.exceptions.FieldError: Unknown filter param 'not_isnull'. Allowed base values are ['attributes', 'created_at', 'id', 'is_active', 'last_modified_at', 'name', 'parent_category', 'parent_category_id', 'product_families', 'slug', 'sub_categories']
Also getting an error with the date filters such as:queryset = queryset.filter(created_at__year=2022)
AttributeError: 'int' object has no attribute 'utcoffset'
Anyone else experiencing this?
Hi folks,
I have the standard Tortoise class User
, which has one special method default_device_group
. This method returns a default device group for the user (the return value is getting from a different source).
class UserPyShow(pydantic_model_creator(User)):
user_groups: List[UserGroupPySimple]
class Config:
extra = Extra.allow
...
@router.get("/me", response_model=UserPyShow)
async def get_my_profile(user=Security(get_me)):
res = (await UserPyShow.from_tortoise_orm(user)).dict()
res['default_device_group'] = await user.default_device_group()
return res
This way is not nice and the swagger (/docs) does not show information about default_device_group
.
Does exist some better way how can I extend Pydantic class?
I have tried to define default_device_group
in UserPyShow
, but always I get a validation error.
Hi I am developing FastApi application with tortoise, postgresql and pytest. I can test my application with sqlite, but when I try to test with PostgreSQL it raises an error:
asyncpg.exceptions._base.InterfaceError: cannot perform operation: another operation is in progress
It first creates a db and tables. Then, tries to execute an insert statement, but fails with this error.
This is my fixture:
@pytest.fixture(scope="session", autouse=True)
def client() -> Generator:
db_url = f"postgres://{settings.POSTGRES_USER}:{settings.POSTGRES_PASSWORD}@{settings.POSTGRES_HOST}:{settings.POSTGRES_PORT}/test_" + "{}"
initializer(DB_MODULES["models"], db_url=db_url, app_label="models")
with TestClient(app) as c:
yield c
finalizer()
It creates the db and tables, but raises error when any request is made
Like this:
@router.post("", response_model=Store_Pydantic)
async def create_store(store: StoreIn_Pydantic):
obj = await StoreModel.create(**store.dict(exclude_unset=True))
return await Store_Pydantic.from_tortoise_orm(obj)
@furkan-guvenc
https://stackoverflow.com/a/69055243/10450923
you can use conftest or just use in your module if all tests for ORM are in one module
Hello, i want to filter a json field containing a list of int
, to find an entry that cointains a value:
class MyModel(Model):
foo = fields.JSONField()
# objects:
await MyModel.create(foo=[1,2,3])
await MyModel.create(foo=[123, 456])
matches = await MyModel.filter(foo__contains=123)
Matches should find the second object. But it contains both objects...
I'm using sqlite