-
-
Notifications
You must be signed in to change notification settings - Fork 16.1k
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
URL Converters should have access to app context #3088
Comments
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: Line 313 in 0b5b4a6
So we'd need to move it into Line 379 in 0b5b4a6
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. |
I managed to accomplish essentially the same thing with decorator functions (and added it on stack overflow), but it would definitely be powerful to be able to swap url parameters with their models. |
Let's start with a PR that makes the change I described, and see if it affects any existing tests. I'm happy to review a PR if someone wants to contribute. |
@davidism Hi! |
It doesn't look like you've submitted a PR. |
ok sorry, a question before I add the PR. def test_model_converters(app, client):
from werkzeug.routing import BaseConverter
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:////tmp/test.db'
db = SQLAlchemy(app)
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(80), unique=True, nullable=False)
email = db.Column(db.String(120), unique=True, nullable=False)
db.create_all()
admin = User(username='admin', email='admin@example.com')
db.session.add(admin)
class ModelConverter(BaseConverter):
def to_python(self, value):
return User.query.filter(User.username == value).first()
def to_url(self, value):
if isinstance(value, str):
return value
return value.username
app.url_map.converters['model'] = ModelConverter
@app.route('/<model:user_name>')
def index(user_name):
return user_name.email
assert client.get('/admin').data == b'admin@example.com' However, I did not get the out of context exception, what am I missing here? |
When using db = SQLAlchemy()
db.init_app(app) |
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. |
Expected Behavior
The following code should create a new URL converter that converts a specific field to a model (or
None
).Actual Behavior
The converters don't have access to the app context so an error is thrown.
Bad Solution
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
Additional Resources
This issue has also come up on stack overflow
The text was updated successfully, but these errors were encountered: