Troubleshooting "represent"

149 views
Skip to first unread message

americandewd

unread,
Jan 19, 2015, 10:26:26 PM1/19/15
to web...@googlegroups.com
How would one go about troubleshooting "represent"?

I am at the point where the ID matches the data I am looking for however it is not showing the title for the matching ID.

Specifically I have categories held in this table:
db.define_table('category',
   
Field('title'),
   
Field('slug',requires=IS_SLUG(),compute=lambda row: IS_SLUG.urlify(row.title)),
   
Field('parent_id','reference category', default=None))

I have a table where these categories will be used:
db.define_table('animals',
   
Field('name'),
   
Field('breed'),
   
Field('category', db.category),
)


I then exclude certain categories I don't want them to be able to post in:
catwanted = []
notwanted
= ['Dogs','Cats','MiscPets']
for row in db(db.category).select():
if row.title in notwanted:
   
pass
else:
    catwanted
.append(row.title)
catwanted
.sort()



Then I have a form where I want to represent the title of the category.id it references so I use represent:
db.animals.category.represent = lambda f: db.category[f].title

Instead of showing me the title it shows me the id of that category when I go to edit the record using crud.update.

I had this issue before however the only difference is that I did not exclude anything, and it was not a dropdown; specifically I used this same exact thing to show me the name of the Animal when updating the status of the pet.

No matter what or where I do with represent it doesn't change the list from the the ID to the title.

I even tried creating this all from scratch and it does exactly the same thing, shows the ID but, not the title of the category.

Anthony

unread,
Jan 19, 2015, 10:37:29 PM1/19/15
to web...@googlegroups.com
The "represent" attribute is used for read-only forms, SQLTABLE, SQLFORM.grid, and when calling rows.render() -- it does not apply to update forms (in that case, the field's widget controls what is displayed).

Anthony

americandewd

unread,
Jan 19, 2015, 11:18:38 PM1/19/15
to web...@googlegroups.com
Thanks for your reply, I am a little confused still though. 

In the book http://web2py.com/books/default/chapter/29/06/the-database-abstraction-layer?search=widget it states for a field constructor I can set "Widget=..." however I did not set any widget for the field.

Since I did not do that, can you please explain further? or where can I read about this in the manual?

Thanks

americandewd

unread,
Jan 19, 2015, 11:48:10 PM1/19/15
to web...@googlegroups.com
I tried this:
Field('category', 'reference category', requires=IS_IN_SET(catwanted,zero=None),represent=lambda catwanted, r: db.category[catwanted].title if catwanted else "")

from what I saw in one of your posts, however it only still shows me the id and not the name being held in the field for that id.

will keep trying and if I succeed I will post here.  Again thanks for any and all help, this problem has been buggin me for a while.


On Monday, January 19, 2015 at 9:37:29 PM UTC-6, Anthony wrote:

americandewd

unread,
Jan 20, 2015, 6:07:47 AM1/20/15
to web...@googlegroups.com
I have not set any widget controls per what the book specifies shows me can be done for the field.

Could you please explain since if I did not set a "widget" then where is this "widget" setting?  I have scoured the book and found nothing other than what I copy/pasted here.

Am I trying to do something unusual?




On Monday, January 19, 2015 at 9:37:29 PM UTC-6, Anthony wrote:

americandewd

unread,
Jan 20, 2015, 7:47:22 AM1/20/15
to web...@googlegroups.com
I read what you're saying however if I make one simple change:

formcontrol.category.writable = False

Then it shows the category name, but not in a dropdown list.

This form is being generated with:
form=crud.update(db.animals,animals,next=url('currentanimals'),onvalidation=my_form_processing)

Which obviously is not SQLFORM, it is CRUD.  So according to what you said I should not be able to use represent, yet there it is working.

Why does commenting this out cause it to not work?
formcontrol.category.writable = False

The dropdown box returns but, without the names and shows the IDs again.  This does not correlate with anything in the book, nor with your statement above.

Is this a bug?




On Monday, January 19, 2015 at 9:37:29 PM UTC-6, Anthony wrote:

Anthony

unread,
Jan 20, 2015, 10:08:50 AM1/20/15
to
On Tuesday, January 20, 2015 at 6:07:47 AM UTC-5, americandewd wrote:
I have not set any widget controls per what the book specifies shows me can be done for the field.

Could you please explain since if I did not set a "widget" then where is this "widget" setting?  I have scoured the book and found nothing other than what I copy/pasted here.

Am I trying to do something unusual?

Every field gets a default widget based on the field type. Furthermore, some validators, such as IS_IN_DB and IS_IN_SET result in particular widgets being used unless overridden via the "widget" argument (or unless the validator is put in a list). In your case, the IS_IN_SET widget results in an HTML <select> element.

Note, reference fields store the integer id values of the records they reference, so you should not use an IS_IN_SET validator with category names as the elements of the set -- instead, the set should be a list of category record id's. You can then pair those id's with labels (i.e., the category names), so the HTML select widget shows the names, but the form actually submits the integer id of the selected category.

Instead, though, this is typically handled with the IS_IN_DB validator, which allows you to define the labels more easily. Note, the first argument to IS_IN_DB can be a DAL Set object defining the records that are allowed (so you can specify the excluded categories).

    Field('category', 'reference category',

          requires
=IS_IN_DB(db(~db.category.title.belongs(notwanted)),
                           
'category.id', label='%(title)s'),
          represent
=lambda category, r: category.title if category else '')

In the above, db(~db.category.title.belongs(notwanted)) defines the set of category records whose title is not in the notwanted list. The validator will select those records and limit valid values for this field to the id's of those records. It will also generate a select widget that displayes the "title" values in the dropdown.

Anthony


americandewd

unread,
Jan 20, 2015, 11:54:07 AM1/20/15
to web...@googlegroups.com
You are THE MAN.  I did not have to do everything you suggested I only had to do this:

formcontrol.category.requires = IS_IN_DB(db(~db.category.title.belongs(notwanted)),'category.id', label='%(title)s')

Just like magic it showed the value in the DB with no problem.

Thank you this was driving me bonkers!!!




On Tuesday, January 20, 2015 at 9:08:50 AM UTC-6, Anthony wrote:
On Tuesday, January 20, 2015 at 6:07:47 AM UTC-5, americandewd wrote:
I have not set any widget controls per what the book specifies shows me can be done for the field.

Could you please explain since if I did not set a "widget" then where is this "widget" setting?  I have scoured the book and found nothing other than what I copy/pasted here.

Am I trying to do something unusual?
Reply all
Reply to author
Forward
0 new messages