URL Converters should have access to app context
See original GitHub issueExpected Behavior
The following code should create a new URL converter that converts a specific field to a model (or None
).
class ModelConverter(BaseConverter):
def to_python(self, value):
return models.Example.query.filter(models.Example.name == value).first()
def to_url(self, value):
if isinstance(value, str):
return value
return value.name
Actual Behavior
The converters don’t have access to the app context so an error is thrown.
Bad Solution
class OrganizationConverter(BaseConverter):
app = None
def to_python(self, value):
with self.app.app_context():
return repositories.Organization.query.filter(repositories.Organization.name == value).first()
def to_url(self, value):
if isinstance(value, str):
return value
return value.name
This solution tries to get around it by creating a new app context. The problem is that the context disappears, killing the database session at the same time. This prevents SQLAlchemy features (such as lazy loading).
Environment
- Python version: 3.6 and 3.7
- Flask version: 1.0.2
- SQLAlchemy version: 1.2.17
- Flask-SQLAlchemy version: 2.3.2
- Werkzeug version: 0.14.1
Additional Resources
This issue has also come up on stack overflow
Issue Analytics
- State:
- Created 5 years ago
- Comments:9 (7 by maintainers)
Top Results From Across the Web
Leveraging the Flask application context in a custom URL ...
This issue is given that the URL converter is outside of the app context I needed to include the following block, with app.app_context():...
Read more >API — Flask Documentation (2.2.x)
The application will work even with __name__ , thanks to how resources are ... The URL adapter is created at a point where...
Read more >Guide for running C# Azure Functions in an isolated worker ...
Learn how to use a .NET isolated worker process to run your C# functions in Azure, which supports non-LTS versions of .NET and...
Read more >Django url parameters, extra options & query strings
Url parameters with Django path converters and Python regular expression named ... Url parameters can capture any part of a url, whether it's...
Read more >Developing Python Web Applications with Flask
You can access the webapp from a browser via URL http://127.0.0.1:5000 (or http://localhost:5000 ). ... 6.5 Flask's Application Context and Request Context.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I’ve also ran into this issue with converters, but it’s might not be straightforward to change at this point.
URL matching is done as part of creating the request context, not pushing the context:
https://github.com/pallets/flask/blob/0b5b4a66ef99c8b91569dd9b9b34911834689d3f/flask/ctx.py#L313
So we’d need to move it into
RequestContext.push
, after it’s actually pushed so thatrequest
is populated:https://github.com/pallets/flask/blob/0b5b4a66ef99c8b91569dd9b9b34911834689d3f/flask/ctx.py#L379
It’s a more internal part of Flask, and I haven’t really seen any discussion about overriding or using these parts, but I’d have to understand that more before making the change.
Is this well documented yet? I’m here since I read the 1.1.1 release notes and was looking for some examples of how this feature could be used. The test above by @eladm26 clears things up but it took awhile for me to find it here.
Here’s the part from the release notes:
Thanks.