Re: [web2py] Convert MongoDB / dict into a DAL Row

117 views
Skip to first unread message

António Ramos

unread,
Nov 14, 2014, 12:10:17 PM11/14/14
to web...@googlegroups.com
Never used mongo but what happens if you assign row.gender  to a var ?


2014-11-14 16:55 GMT+00:00 Francisco Costa <ma...@franciscocosta.com>:
Hi,

I'm unable to fully convert a dict into a Row(), specially if there is a foreign key (ObjectId)

I have the following code (I've commented the prints):

db = DAL(
   
'mongodb://localhost/%s' % request.application,
    pool_size
=10,
    adapter_args
={'safe': False},
    check_reserved
=['all']
)

db
.define_table(
   
'genders',
   
Field('id', required=True, unique=True),
   
Field('name', 'string', length=128, required=True),
    format
='%(name)s'
)

db
.define_table(
   
'auth_user',
   
Field('id', required=True, unique=True, writable=False, readable=False),
   
Field('name', 'string', length=128, required=True),
   
Field('gender', db.genders, required=False, default=None),
    format
='%(name)s'
)

from gluon.dal import Row

mongo
= db._adapter.connection  # Use PYMONGO

user
= mongo.auth_user.find_one({'name': 'Francisco Costa'})
print(user)
#    {
#        u'name': u'Francisco Costa'
#        u'city': u'Porto',
#        u'gender': ObjectId('51c1e89a03d36d6fa90ce06c')
#    }
print(Row(user))
#    <Row {
#        u'name': u'Francisco Costa',
#        u'city': u'Porto'
#    }>
user
= db(db.auth_user.name=='Francisco Costa').select().first()
print(user)
#    <Row {
#        'name': 'Francisco Costa',
#        'city': 'Porto'
#        'gender': 25302706908812871434930806892L,
#    }>


As you can see when I  print(Row(user)) the gender is not shown.

How to solve this?

--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to the Google Groups "web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to web2py+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Leonel Câmara

unread,
Nov 14, 2014, 1:40:17 PM11/14/14
to web...@googlegroups.com
Why are you talking to mongodb directly? This completely ruins the purpose of using a DAL.

What's happening here is that when you do: 

print(Row(user)) 

Row.__str__ is called which in turn calls Row.as_dict, well Row.as_dict does not know how to serialize ObjectId so it just ignores it.

One thing you can do is to convert the value of ObjectId before putting it in the Row.

Frankly this seems all sorts of wrong, and you should just use the last syntax you have shown which does this conversion for you and does work.

Alan Etkin

unread,
Nov 15, 2014, 8:02:00 AM11/15/14
to web...@googlegroups.com
Row.__str__ is called which in turn calls Row.as_dict, well Row.as_dict does not know how to serialize ObjectId so it just ignores it.

I don't recall exactly (perhaps I should check the source), but I think the mongodb adapter doesn't support dealing with pure objectid objects for database I/O. What it does is always convert from/to long integers because objectid objects are mapped to web2py db record ids. So, you should not be able to work with objectid instances and DAL unless you only store and retrieve id values.

@Francisco: about your question on cache not working with mongo, I wouldn't say, maybe there is a bug in the adapter. AFAIK the caching of rows on selects should work with mongodb as well.

António Ramos

unread,
Nov 17, 2014, 1:05:27 PM11/17/14
to web...@googlegroups.com
By the way, how is web2py support for Mongo ?
I only find old post about it.

Is it safe to use Mongo with Web2py ?

Is the DAL 100% compatible with mongo ?

Regards
António


--

Leonel Câmara

unread,
Nov 18, 2014, 5:14:53 AM11/18/14
to
What if you use limitby in the select to limit the results to just one, is there still a performance difference?
Reply all
Reply to author
Forward
Message has been deleted
Message has been deleted
0 new messages