Remove _extra from as_list()

118 views
Skip to first unread message

Eric

unread,
Jul 20, 2016, 10:00:57 AM7/20/16
to web2py-users
Hi There,

Is there a way in Web2Py to suppress the _extra information when selecting data with as_list() or as_dict()?

I can't find it and option for it, but maybe I'm just overlooking it in the documentation ;)

Thanks!

Anthony

unread,
Jul 20, 2016, 2:56:54 PM7/20/16
to web2py-users
The "_extra" dictionary stores values generated from expressions (rather than raw values from individual fields). If you don't want them to appear in the conversion of the Rows to a list of dictionaries, then don't include them in the original .select() call that generated the Rows object.

Anthony

Eric Christiaanse

unread,
Jul 20, 2016, 3:55:19 PM7/20/16
to web...@googlegroups.com
Hi Anthony,

I'm not including them. I'm only using with_alias() and the select in combination with as_list(). I would not expect to get the _extra as a little bonus when you use with_alias()...

That's why I'm asking :)

Greetings,

Eric


--
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 a topic in the Google Groups "web2py-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/web2py/d3YmI1e3iaQ/unsubscribe.
To unsubscribe from this group and all its topics, send an email to web2py+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Anthony

unread,
Jul 21, 2016, 11:12:16 AM7/21/16
to web2py-users
Please show your code.
To unsubscribe from this group and all its topics, send an email to web2py+unsubscribe@googlegroups.com.

Eric Christiaanse

unread,
Jul 22, 2016, 1:56:34 AM7/22/16
to web...@googlegroups.com
Hi Anthony,

My code is the following:

# -*- coding: utf-8 -*-
from gluon.serializers import json


@auth.requires(lambda: AuthHelper.valid_token(), requires_login=False)
@request.restful()
def search():
    session.forget()

    def post(*args, **vars):
        response.headers["Content-Type"] = "application/json"
        retval, error = RequestHelper.parse(request)

        if error is not None:
            return json(error)

        encoded_zip = int(StringHelper.encode_postcode(retval["zip_code"]))
        house_number = int(retval["house_number"])

        return json(dict(result=db((db.addresses.encoded_zip_code == encoded_zip) & (db.addresses.house_number == house_number)).select(
            db.addresses.street.with_alias('straat'),
            db.addresses.city.with_alias('plaats),
            distinct=True).as_list()))

    return dict(GET=post, POST=post)



To unsubscribe from this group and all its topics, send an email to web2py+un...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--
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 a topic in the Google Groups "web2py-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/web2py/d3YmI1e3iaQ/unsubscribe.
To unsubscribe from this group and all its topics, send an email to web2py+un...@googlegroups.com.

Anthony

unread,
Jul 22, 2016, 2:59:12 PM7/22/16
to web2py-users
Got it. Unfortunately, I don't think there is a way to prevent the "_extra" key from being added to each Row in this case, so .as_list() will end up including it. You'll either have to remove the "_extra" keys after calling .as_list(), or write your own function to convert the Rows to a list of dictionaries.

Feel free to submit a Github issue (to the PyDAL repo, not the web2py repo) suggesting that "with alias" fields not be added to the "_extra" dictionary (it appears to be unnecessary, as the alias is already added as a top level attribute of the Row object).

Anthony
To unsubscribe from this group and all its topics, send an email to web2py+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--
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 a topic in the Google Groups "web2py-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/web2py/d3YmI1e3iaQ/unsubscribe.
To unsubscribe from this group and all its topics, send an email to web2py+unsubscribe@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages