Middleware for Google NDB
See original GitHub issueI am trying to create a middleware for Google NDB. It requires the use of an context / the wrapping of the actual code within a “with” statement. It is recommended to create a single context for each HTTP Request. E.g.:
app = FastAPI()
client = ndb.Client()
class Contact(ndb.Model):
name = ndb.StringProperty()
phone = ndb.StringProperty()
email = ndb.StringProperty()
@app.get("/create")
def create():
with client.context():
contact1 = Contact(name="John Smith",
phone="555 617 8993",
email="john.smith@gmail.com")
contact1.put()
I tried to implement the “with-wrapping” within a middleware:
@app.middleware("http")
async def add_datastore(request: Request, call_next):
with client.context():
response = await call_next(request)
return response
@app.get("/create_no_ctx")
def create_no_ctx():
contact1 = Contact(name="John Smith",
phone="555 617 8993",
email="john.smith@gmail.com")
contact1.put()
This middleware has no effect. Calling the create_no_ctx route yields a NDB-Exception that no context was set. Is is possible to create a middleware that wraps all route functions in a with statement with the context?
Issue Analytics
- State:
- Created 4 years ago
- Comments:7 (3 by maintainers)
Top Results From Across the Web
Migrating to Cloud NDB - App Engine
Instead, you can easily write your own middleware with just a few lines of code. App Engine NDB required apps and the Datastore...
Read more >Django Middleware — ndb documentation - Google Cloud
Django middleware for ndb . class google.cloud.ndb.django_middleware. NdbDjangoMiddleware (*args, **kwargs)[source]¶. Bases: object ...
Read more >How to configure google app engine datastore (NDB) with ...
I am trying to use google datastore (NDB) with Django. This is the error I get when I try to put an entity....
Read more >NDB overview docs show incorrect Django middleware path ...
The Django section in https://developers.google.com/appengine/docs/python/ndb/overview has a reference to the middleware that needs adding to settings.py.
Read more >ModelForms for ndb model appengine (djangoforms not ...
Apart from that you need to add a middleware: 'google.appengine.ext.ndb.NdbDjangoMiddleware'. Doing the above two will fix things.
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 FreeTop 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
Top GitHub Comments
I created a pull request with the fix according to https://www.python.org/dev/peps/pep-0567/#converting-code-that-uses-threading-local It really works fine and I hope it will be merged
Opps this is a won’t fix on their end because of possible alternatives: https://github.com/googleapis/python-ndb/issues/289