python - MongoDB & web2py: Working with ObjectIds -
I'm working on a very simple application as a case of use to integrate Mongo DB with web2py I am In one part of the application, I am interested in returning a list of products:
My database table:
db.define_table ('product', field ('field ( 'Photo', label = 'photos'), ... field ('option', label = 'options'))
My Controller:
Def products (): qset = db (db ['products']) grid = qset.select () return dict (grid = grid)
My view:
< Pre> {{extension 'layout.html'}} & lt; h2 & gt; Product List & lt; / h2 & gt; {{= Grid}}
Products Returned without problems However, the products._id returns the value as '26086541625969213357181461154' if I switch to the shell (or python) and try to query my database based on those IDs, then I have no product You can not get it.
As you expect, the objects given in the database are ObjectIds that look like '544a481b2ceb7c3093a173a2', I do not want to return the object IDs and do not wired for long I want to think. Simple, but I'm having trouble with it
DAL for a given MongoDB record When forming an object, ObjectId is displayed by converting
long
long (str (value), 16)
through the integer long
integer. To convert back to an ObjectId, you can use the MongoDB adapter's object_id
method:
object_id = db._adapter.object_id ('26086541625969213357181461154')
Of course, if you use DAL to ask Mangodybi, then you do not have to worry about it because it automatically handles the conversion.
Comments
Post a Comment