Re: [dabo-users] Populating a dropdown list

Author: John Fabiani

Posted: 2012-07-24 at 20:58:53

On 07/21/2012 11:20 AM, Dave Kelly wrote:

> Trying to follow the PyCon tutorial, I am attempting to populate a dropdown

> list. The underlying table for the dropdown list contains only one field

> which is the primary key. I can't seem to get the list to populate. The

> steps I took to achieve this were:

>

>

>

> 1. I created a script in the /biz directory called

> PublicdepartmentsBizobj.py which contains:

>

>

>

> #!/usr/bin/env python

>

> # -*- coding: utf-8 -*-

>

>

>

> import dabo

>

>

>

> class PublicdepartmentsBizobj(dabo.biz.dBizobj):

>

> def afterinit(self):

>

> self.datasource = "public.departments"

>

> self.keyfield = "department"

>

> self.addfrom("public.departments")

>

> self.addfield("department")

>

>

>

> def validateRecord(self):

>

> """Returning anything other than an empty

> string from

>

> this method will prevent the data from being

> saved.

>

> """

>

> ret = ""

>

> # Add your business rules here.

>

> return ret

>

>

>

> def getNamesAndKeys(self):

>

> crs = self.getTempCursor()

>

> crs.execute("select department from

> public.departments order by department")

>

> ds = crs.getDataSet()

>

> names = [rec["department"] for rec in ds]

>

> keys = [rec["department"] for rec in ds]

>

>

>

> return(names, keys)

>

>

>

> My createBizobjs() method for the form contains:

>

>

>

> def createBizobjs(self):

>

> publicemployeesBizobj =

> self.Application.biz.PublicemployeesBizobj(self.Connection)

>

> self.addBizobj(publicemployeesBizobj)

>

> publicdepartmentsBizobj =

> self.Application.biz.PublicdepartmentsBizobj(self.Connection)

>

> self.addBizobj(publicdepartmentsBizobj)

>

>

>

>

>

> I set the DataSource property of my dropdownlist control to

> "public.departments". I set the DataField property to "department". The

> ValueMode is set to "Key" (I also tried "String"). RegID is set to

> "department_list".

>

>

>

> The afterInitAll method is as follows:

>

>

>

> def afterInitAll(self):

>

> departmentBiz = self.getBizobj("public.departments")

>

> names, keys = departmentBiz.getNamesAndKeys()

>

> self.department_list.Choices = names

>

> self.department_list.Keys = keys

>

> self.department_list.ValueMode = "Key"

>

> self.requery()

>

>

>

>

>

> I closed the ClassDesigner and updated my main.py to the following:

>

>

>

> import dabo

>

> dabo.ui.loadUI("wx")

>

>

>

> app = dabo.dApp()

>

>

>

> app.MainFormClass = "time_clock.cdxml"

>

>

>

> app.start()

>

>

>

> The form loads but the dropdown list is empty. Furthermore, the console has

> the following output. I am sure I have missed something blatently obvious

> but I wil need it pointed out to me.

>

>

>

> dave@dave-Mint ~/time $ python main.py

>

> /usr/local/lib/python2.7/dist-packages/dabo/lib/SimpleCrypt.py:52:

> UserWarning: WARNING: SimpleCrypt is not secure. Please see

> http://wiki.dabodev.com/SimpleCrypt for more information

>

> warnings.warn("WARNING: SimpleCrypt is not secure. Please see

> http://wiki.dabodev.com/SimpleCrypt for more information")

>

> Traceback (most recent call last):

>

> File "/usr/lib/python2.7/dist-packages/wx-2.8-gtk2-unicode/wx/_core.py",

> line 14636, in<lambda>

>

> lambda event: event.callable(*event.args, **event.kw) )

>

> File "/usr/local/lib/python2.7/dist-packages/dabo/ui/uiwx/dPemMixin.py",

> line 348, in _afterInitAll

>

> self.afterInitAll()

>

> File "/tmp/tmpQOkWFW.py", line 173, in afterInitAll

>

> names, keys = departmentBiz.getNamesAndKeys()

>

> AttributeError: 'NoneType' object has no attribute 'getNamesAndKeys'

>

>

>

> Dave Kelly

>

> The Priory Inn

>

> 01666 502 251

>

Sorry on vacation until the 30th. Also to be honest I can't recall what

the demo does?

I believe you should change the following:

self.addfrom("public.departments")

self.addfrom("departments") #that is the normal coding.

publicdepartmentsBizobj =

self.Application.biz.PublicdepartmentsBizobj(self.Connection)

self.publicdepartmentsBizobj = PublicdepartmentsBizobj(self.Connection)

self.addBizobj(self.publicdepartmentsBizobj)

#using "self" allows you access and you can avoid the getBizobj()

# should also fix the error

names, keys = self.publicdepartmentsBizobj.getNamesAndKeys()

self.department_list.Choices = names

self.department_list.Keys = keys

self.department_list.ValueMode = "Key"

self.requery() #watch out here what are you requerying????

Can I assume that you want both the Choices and and Keys to match? Normally, you would use some PK (normally an int) with some description text.

names = [rec["department"] for rec in ds]

keys = [rec["department"] for rec in ds]

they are retrieving the same data. If so why use the keys?

The dropdown list is sort of different - in that it requires that a value always exist for the record (even new ones). So I do something like this.

def countyChoices(self):

countyDS = self.escounty.getDataSet()

county_Choices=['<None>']

countyKeys=[0]

Now I have a '<None>' choice for key '0' that will match a new record value,

The above said I do not see a data issue.

Johnf

_______________________________________________

Post Messages to: Dabo-users@leafe.com

Subscription Maintenance: http://leafe.com/mailman/listinfo/dabo-users

Searchable Archives: http://leafe.com/archives/search/dabo-users

This message: http://leafe.com/archives/byMID/500F52DD.8090208@jfcomputer.com

©2012 John Fabiani