Virtual Fields not working on print.

46 views
Skip to first unread message

Jason Brower

unread,
Feb 19, 2014, 3:40:52 AM2/19/14
to web2py-users
I have this in the model...
import uuid
import os
import pygeoip
gi = pygeoip.GeoIP(os.path.join(request.folder, 'private', 'GeoIP.dat'))
def get_country(row):
return gi.country_code_by_addr(row.from_where)
# -*- coding: utf-8 -*-
db.define_table('melodigram_play',
Field('melodigram_id', 'reference melodigram'),
Field('when_opened', 'datetime', default = request.now),
Field('from_where', 'string', default = request.client),
Field.Virtual('from_country', lambda row: get_country(row))
)
db.melodigram_play.melodigram_id.requires = IS_NOT_EMPTY()
db.melodigram_play.when_opened.requires = IS_NOT_EMPTY()

I can insert data... but when I try to retrieve it, it tells me the row
doesn't exist:
<h2>From: {{=gram_details.from_where}} :
{{=gram_details.from_country}}</h2>
----
gram_details.from_where has always worked.
gram_details.from_country doesn't work.


Tim Richardson

unread,
Feb 19, 2014, 5:58:05 AM2/19/14
to web...@googlegroups.com
Can you upload your app?

Marin Pranjić

unread,
Feb 19, 2014, 6:47:45 AM2/19/14
to web2py-users
most likely get_country is failing for some reason

Marin




--
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/groups/opt_out.

Encompass solutions

unread,
Feb 19, 2014, 7:32:41 AM2/19/14
to web...@googlegroups.com
To try to triage the bug I did the following...
import os
import pygeoip
#gi = pygeoip.GeoIP(os.path.join(request.folder, 'private', 'GeoIP.dat'))

def get_country(row):
    return "what?"

# -*- coding: utf-8 -*-
db.define_table('melodigram_play',
    Field('melodigram_id', 'reference melodigram'),
    Field('when_opened', 'datetime', default = request.now),
    Field('from_where', 'string', default = request.client),
    Field.Virtual('from_country', lambda row: get_country(row))
    )
db.melodigram_play.melodigram_id.requires = IS_NOT_EMPTY()
db.melodigram_play.when_opened.requires = IS_NOT_EMPTY()
-----------------------------------------------------------------------------------------------
Basically I just remove everything and returned a text object.
This still gives me the same error.
  File "/home/encompass/Projects/melodigram/web2py/applications/melodigram/views/administration/browser.html", line 101, in <module>
File "/home/encompass/Projects/melodigram/web2py/gluon/dal.py", line 7267, in __getitem__
raise ae
AttributeError: 'Row' object has no attribute 'from_country'

Anthony

unread,
Feb 19, 2014, 8:59:29 AM2/19/14
to
In virtual field functions, you must refer to the table name and field name within each row object, so:

        return gi.country_code_by_addr(row.from_where)

should be:

        return gi.country_code_by_addr(row.melodigram_play.from_where)

That doesn't explain the problem below, but I wonder if something else is going on there (e.g., is the app compiled, and you didn't re-compile after making the change?).

As an aside, if you already have a function, no need to wrap it in a lambda -- just do:

Field.Virtual('from_country', get_country)

Anthony

Jason (spot) Brower

unread,
Feb 23, 2014, 11:49:47 AM2/23/14
to web2py-users
Yes, that seems to do it, thanks. :)


On Wed, Feb 19, 2014 at 3:58 PM, Anthony <abas...@gmail.com> wrote:
In virtual field functions, you must refer to the table name and field name within each row object, so:

        return gi.country_code_by_addr(row.melodigram_play.from_where)

should be:

        return gi.country_code_by_addr(row.from_where)

That doesn't explain the problem below, but I wonder if something else is going on there (e.g., is the app compiled, and you didn't re-compile after making the change?).

As an aside, if you already have a function, no need to wrap it in a lambda -- just do:

Field.Virtual('from_country', get_country)

Anthony


On Wednesday, February 19, 2014 7:32:41 AM UTC-5, Encompass solutions wrote:
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/2Shkz7T3cvQ/unsubscribe.
To unsubscribe from this group and all its topics, send an email to web2py+un...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages