I'm trying to build a default display name from the table/fields below 
depending on person.type and
whether or not the person.referrer_rf is populated and I am trying two 
different methods 

1> a compute field and 
2> a virtual field

Either would probably suffice if can get one to work...


e.g. Database content
id    first_name    last_name    person_type    referrer_ref    short_name
1    John            Doe              CLIENT          TH/256          Null  
    
2    Jane            Doe              CLIENT          7845             Null 
  
3    Jack            Doe              CLIENT                              
Null
4    Jill               Doe              CONTACT                          
Null


Required output - Expected Shortname (or Displayname)
id    
1    TH/256 John
2    7845 Jane
3    PAP/3 Jack
4    Doe, Jill 






I have this function in the model

def get_user_short_name(id, type, referrer_ref, first_name, last_name):

    display_name = "NAME ERROR - unknown person type"
    
    if type == 'CLIENT':
        if referrer_ref:
            display_name = '%s %s' % (referrer_ref, first_name)
        else:
            display_name = 'PAP/%s %s' % (id, first_name)
    
    elif type == 'CONTACT':
        display_name = '%s, %s' % (last_name, first_name)

    return display_name


and this person table

db.define_table('person',
                Field('person_type', 
requires=IS_IN_SET(PERSON_TYPES),default='CLIENT',
                      label='ContactType',
                      comment="* CLIENT for Clients otherwise CONTACT."),
                Field('first_name', requires=IS_NOT_EMPTY(), comment="*", 
label='FirstName'),
                Field('last_name', requires=IS_NOT_EMPTY(), comment="*", 
label='LastName'),
                Field('short_name',
                      compute=lambda row: get_user_short_name(row.id, 
                                                              
row.person_type, 
                                                              
row.referrer_ref, 
                                                              
row.first_name, 
                                                              
row.last_name),
                      label='ShortName',
                      comment="Computed",
                      writable=False),
        [...]
                Field('referrer_ref', 'string', default=None, 
label="ReferrersCode",
                      comment="Referring Company's Client ID if provided, 
otherwise leave blank"),
        [...]
                Field('date_of_birth', 'date', 
requires=IS_EMPTY_OR(IS_DATE(format='%d-%m-%Y',
                      error_message = 'must be in DD-MM-YYYY format'))),
        [...]
                format='%(last_name)s %(first_name)s %(referrer_ref)s')


db.person.virtual_age = Field.Virtual('virtual_age',
                                      lambda row: 
age_in_years(row.person.date_of_birth))

db.person.display_name = Field.Virtual('display_name',
                                       lambda row: 
get_user_short_name(row.id,
                                                                       
row.person_type,
                                                                       
row.referrer_ref,
                                                                       
row.first_name,
                                                                       
row.last_name))




When I add code to display the contents    'Shortname: %s' % 
person.short_name 
(even after I have edited the table - which I understand should trigger the 
shortname update)
I get     'ShortName: None'



and for the seond method    'DisplayName: %s' % person.display_name

I get an attribute error...

Traceback (most recent call last):
  File "/home/peter/web2py/gluon/restricted.py", line 227, in restricted
    exec ccode in environment
  File 
"/home/peter/web2py/applications/PAPILLON_AIM/controllers/default.py", line 
2256, in <module>
  File "/home/peter/web2py/gluon/globals.py", line 417, in <lambda>
    self._caller = lambda f: f()
  File "/home/peter/web2py/gluon/tools.py", line 4241, in f
    return action(*a, **b)
  File 
"/home/peter/web2py/applications/PAPILLON_AIM/controllers/default.py", line 
305, in view_person
    'DisplayName: %s' % person.display_name
  File "/home/peter/web2py/gluon/packages/dal/pydal/objects.py", line 90, 
in __getattr__
    raise AttributeError
AttributeError

 
No idea why the compute one is not working as it is similar to the 
person.virtual_age that works fine.

The only other thing I can think to add is that I am developing using an 
SQLite database and the short_name 
field was pre-existing before I decided to usurp it for this method. 
Perhaps there is a migrate issue? 


Any ideas on what I am doing wrong would be very welcome!

-- 
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+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to