Comments (8)
@ChengChe106 Sorry I wrote that in a hurry. I have edited my comment, let me know if you have any questions.
from sqladmin.
Hey, thanks for reporting this.
- First thing is that you can pass your session_maker to
Admin(session_maker=...)
with theexpire_on_commit
so SQLAdmin doesn't have to create one for you. - Second is that there's no code and information in your description to know how this is happening.
from sqladmin.
I can confirm this. This can be easily reproduced if you try using any field from the relation in __repr__
method. When you open any admin pane where this representation is shown, you will get the following:
sqlalchemy.orm.exc.DetachedInstanceError: Parent instance <Release at 0x115b20690> is not bound to a Session; lazy load operation of attribute 'product' cannot proceed
from sqladmin.
Yes, I did reconstruct the method repr of models in my project. I was unaware that this was the issue.
from sqladmin.
if you try using any field from the relation in
That is expected. The SQLAlchemy session is closed at this point so you cannot load any relationship that was not loaded.
For example if your model has 10 relationships and SQLAdmin has no idea which relationships to pre-load, it has to either load nothing or load all of them to avoid this problem. Loading all of them will be a huge database load.
I can think of two options for this:
- If your model has a relationship called
x
, you can includex
incolumn_list
andcolumn_details_list
. This way the relationship is always loaded and should work in__repr__
too. - Another option is that we allow the user to control which relationships to load so something along these lines should be updated:
Line 648 in d062cfa
from sqladmin.
Pardon, I didn't understand the sentence "SQLAdmin should not relationships so they can be used in the repr or str methods." Could you please explain it?
from sqladmin.
if you try using any field from the relation in
That is expected. The SQLAlchemy session is closed at this point so you cannot load any relationship that was not loaded. For example if your model has 10 relationships and SQLAdmin has no idea which relationships to pre-load, it has to either load nothing or load all of them to avoid this problem. Loading all of them will be a huge database load.
I can think of two options for this:
- If your model has a relationship called
x
, you can includex
incolumn_list
andcolumn_details_list
. This way the relationship is always loaded and should work in__repr__
too.- Another option is that we allow the user to control which relationships to load so something along these lines should be updated:
Line 648 in d062cfa
I try the first option. It cann't work.
If I add "User.permission_groups" to the "column_list" and "column_details_list" in here, like this:
class UserAdmin(ModelView, model=User):
column_list = [
User.id,
User.username,
User.is_active,
User.is_superuser,
User.permission_groups,
]
column_column_details_list = [
User.id,
User.username,
User.is_active,
User.is_superuser,
User.permission_groups,
]
This error is raised:
ERROR: Exception in ASGI application
Traceback (most recent call last):
File "F:\PythonProjects\FastAPI\FastAPISystem\venv\lib\site-packages\uvicorn\protocols\http\httptools_impl.py", line 426, in run_asgi
result = await app( # type: ignore[func-returns-value]
File "F:\PythonProjects\FastAPI\FastAPISystem\venv\lib\site-packages\uvicorn\middleware\proxy_headers.py", line 84, in __call__ return await self.app(scope, receive, send)
File "F:\PythonProjects\FastAPI\FastAPISystem\venv\lib\site-packages\fastapi\applications.py", line 1054, in __call__
await super().__call__(scope, receive, send)
File "F:\PythonProjects\FastAPI\FastAPISystem\venv\lib\site-packages\starlette\applications.py", line 116, in __call__
await self.middleware_stack(scope, receive, send)
File "F:\PythonProjects\FastAPI\FastAPISystem\venv\lib\site-packages\starlette\middleware\errors.py", line 186, in __call__
raise exc
File "F:\PythonProjects\FastAPI\FastAPISystem\venv\lib\site-packages\starlette\middleware\errors.py", line 164, in __call__
await self.app(scope, receive, _send)
File "F:\PythonProjects\FastAPI\FastAPISystem\venv\lib\site-packages\starlette\middleware\exceptions.py", line 62, in __call__
await wrap_app_handling_exceptions(self.app, conn)(scope, receive, send)
File "F:\PythonProjects\FastAPI\FastAPISystem\venv\lib\site-packages\starlette\_exception_handler.py", line 55, in wrapped_app
raise exc
File "F:\PythonProjects\FastAPI\FastAPISystem\venv\lib\site-packages\starlette\_exception_handler.py", line 44, in wrapped_app
await app(scope, receive, sender)
File "F:\PythonProjects\FastAPI\FastAPISystem\venv\lib\site-packages\starlette\routing.py", line 746, in __call__
await route.handle(scope, receive, send)
File "F:\PythonProjects\FastAPI\FastAPISystem\venv\lib\site-packages\starlette\routing.py", line 459, in handle
await self.app(scope, receive, send)
File "F:\PythonProjects\FastAPI\FastAPISystem\venv\lib\site-packages\starlette\applications.py", line 116, in __call__
await self.middleware_stack(scope, receive, send)
File "F:\PythonProjects\FastAPI\FastAPISystem\venv\lib\site-packages\starlette\middleware\errors.py", line 186, in __call__
raise exc
File "F:\PythonProjects\FastAPI\FastAPISystem\venv\lib\site-packages\starlette\middleware\errors.py", line 164, in __call__
await self.app(scope, receive, _send)
File "F:\PythonProjects\FastAPI\FastAPISystem\venv\lib\site-packages\starlette\middleware\sessions.py", line 83, in __call__
await self.app(scope, receive, send_wrapper)
File "F:\PythonProjects\FastAPI\FastAPISystem\venv\lib\site-packages\starlette\middleware\exceptions.py", line 62, in __call__
await wrap_app_handling_exceptions(self.app, conn)(scope, receive, send)
File "F:\PythonProjects\FastAPI\FastAPISystem\venv\lib\site-packages\starlette\_exception_handler.py", line 55, in wrapped_app
raise exc
File "F:\PythonProjects\FastAPI\FastAPISystem\venv\lib\site-packages\starlette\_exception_handler.py", line 44, in wrapped_app
await app(scope, receive, sender)
File "F:\PythonProjects\FastAPI\FastAPISystem\venv\lib\site-packages\starlette\routing.py", line 746, in __call__
await route.handle(scope, receive, send)
File "F:\PythonProjects\FastAPI\FastAPISystem\venv\lib\site-packages\starlette\routing.py", line 288, in handle
await self.app(scope, receive, send)
File "F:\PythonProjects\FastAPI\FastAPISystem\venv\lib\site-packages\starlette\routing.py", line 75, in app
await wrap_app_handling_exceptions(app, request)(scope, receive, send)
File "F:\PythonProjects\FastAPI\FastAPISystem\venv\lib\site-packages\starlette\_exception_handler.py", line 55, in wrapped_app
raise exc
File "F:\PythonProjects\FastAPI\FastAPISystem\venv\lib\site-packages\starlette\_exception_handler.py", line 44, in wrapped_app
await app(scope, receive, sender)
File "F:\PythonProjects\FastAPI\FastAPISystem\venv\lib\site-packages\starlette\routing.py", line 70, in app
response = await func(request)
File "F:\PythonProjects\FastAPI\FastAPISystem\venv\lib\site-packages\sqladmin\authentication.py", line 66, in wrapper_decorator return await func(*args, **kwargs)
File "F:\PythonProjects\FastAPI\FastAPISystem\venv\lib\site-packages\sqladmin\application.py", line 444, in list
return await self.templates.TemplateResponse(
File "F:\PythonProjects\FastAPI\FastAPISystem\venv\lib\site-packages\sqladmin\templating.py", line 63, in TemplateResponse
content = await template.render_async(context)
File "F:\PythonProjects\FastAPI\FastAPISystem\venv\lib\site-packages\jinja2\environment.py", line 1324, in render_async
return self.environment.handle_exception()
File "F:\PythonProjects\FastAPI\FastAPISystem\venv\lib\site-packages\jinja2\environment.py", line 936, in handle_exception
raise rewrite_traceback_stack(source=source)
File "F:\PythonProjects\FastAPI\FastAPISystem\venv\lib\site-packages\jinja2\environment.py", line 1321, in <listcomp>
[n async for n in self.root_render_func(ctx)] # type: ignore
File "F:\PythonProjects\FastAPI\FastAPISystem\venv\lib\site-packages\sqladmin\templates\list.html", line 1, in top-level template code
{% extends "layout.html" %}
File "F:\PythonProjects\FastAPI\FastAPISystem\venv\lib\site-packages\sqladmin\templates\layout.html", line 2, in top-level template code
{% from '_macros.html' import display_menu %}
File "F:\PythonProjects\FastAPI\FastAPISystem\venv\lib\site-packages\sqladmin\templates\base.html", line 18, in top-level template code
{% block body %}
File "F:\PythonProjects\FastAPI\FastAPISystem\venv\lib\site-packages\sqladmin\templates\layout.html", line 49, in block 'body'
{% block content %} {% endblock %}
File "F:\PythonProjects\FastAPI\FastAPISystem\venv\lib\site-packages\sqladmin\templates\list.html", line 147, in block 'content'
<a href="{{ model_view._build_url_for('admin:details', request, elem) }}">({{ formatted_elem }})</a>
File "F:\Python310\lib\dataclasses.py", line 239, in wrapper
result = user_function(self)
File "<string>", line 3, in __repr__
File "F:\PythonProjects\FastAPI\FastAPISystem\venv\lib\site-packages\sqlalchemy\orm\attributes.py", line 566, in __get__
return self.impl.get(state, dict_) # type: ignore[no-any-return]
File "F:\PythonProjects\FastAPI\FastAPISystem\venv\lib\site-packages\sqlalchemy\orm\attributes.py", line 1086, in get
value = self._fire_loader_callables(state, key, passive)
File "F:\PythonProjects\FastAPI\FastAPISystem\venv\lib\site-packages\sqlalchemy\orm\attributes.py", line 1121, in _fire_loader_callables
return self.callable_(state, passive)
File "F:\PythonProjects\FastAPI\FastAPISystem\venv\lib\site-packages\sqlalchemy\orm\strategies.py", line 918, in _load_for_state
raise orm_exc.DetachedInstanceError(
sqlalchemy.orm.exc.DetachedInstanceError: Parent instance <PermissionGroup at 0x2450fee7e50> is not bound to a Session; lazy load operation of attribute 'users' cannot proceed (Background on this error at: https://sqlalche.me/e/20/bhk3)
If I don't add "User.permission_groups" to column_list, users list page is good. But, no matter how, visiting users detail page will raise the error mentioned above.
from sqladmin.
Please provide a minimum code to reproduce the issue. I see that you are using dataclasses but don't see how.
from sqladmin.
Related Issues (20)
- Sort symbol is incorrect (wrong direction)
- sqladmin + jinja templates conflict index.html HOT 3
- Raised value error HOT 5
- Define default values via form_args
- TypeError: UUID objects are immutable (fastapi_users) HOT 4
- ValueError on passing unexcepted parameter
- Use HTMX HOT 3
- jinja2.exceptions.TemplateNotFound: list.html HOT 3
- What about Router per View? HOT 2
- [feature request] Custom login page HOT 1
- Page size selection at bottom sometimes takes the user to non existing page
- Ckeditor <TypeError: Cannot convert undefined or null to object> HOT 1
- Specify particular column to be searched. HOT 1
- sqlalchemy column_property should be ignore when create/update HOT 10
- Localization support HOT 1
- Datetime with timezone edited incorrectly HOT 1
- Image corruption when editing other fields
- Adding custom filtering for relationships ModelConverterBase._prepare_select_options
- Redirect to custom url after creation HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from sqladmin.