Re: [dabo-users] Setting DropdownList.Value exception

Author: Miguel Lopes

Posted: 2009-07-27 at 09:38:35

On Mon, Jul 27, 2009 at 2:05 PM, Paul McNett<p@ulmcnett.com> wrote:

> Miguel Lopes wrote:

>> This case maybe special because the form is loaded from a cdxml file.

>> Note that properties are not being set in the class designer (as per

>> my email above).

>>

>> In this case dropdownList properties are being set in the afterInitAll

>> cdxml form method. The code is:

>>

>> def afterInitAll(self):

>> =A0 =A0 =A0 _valueListProvider =3D self.Application.getAppInfo('valueLis=

tProvider')

>> =A0 =A0 =A0 vlKeys, vlChoices =3D _valueListProvider.getKeysAndChoices('=

contact_title')

>> =A0 =A0 =A0 dropDown =3D self.getObjectByRegID("dlTitle")

>> =A0 =A0 =A0 dropDown.DataSource=3D"contact"

>> =A0 =A0 =A0 dropDown.DataField=3D"title_id"

>> =A0 =A0 =A0 dropDown.Keys =3D vlKeys

>> =A0 =A0 =A0 dropDown.Choices =3D vlChoices

>> =A0 =A0 =A0 dropDown.ValueMode=3D"Key" =A0 =A0 =A0# if both commented ru=

ns ok - but not

>> in 'key' valueMode

>> =A0 =A0 =A0 dropDown.Value =3D vlKeys[0] =A0 =A0 =A0 # if both commented=

runs ok - but

>> not in 'key' valueMode

>

> What happens with:

>

> def afterInitAll(self):

> =A0 =A0 =A0 =A0...

> =A0 =A0 =A0 =A0dropDown.Keys =3D vlKeys

> =A0 =A0 =A0 =A0dropDown.Choices =3D vlChoices

> =A0 =A0 =A0 =A0dropDown.ValueMode =3D "key"

> =A0 =A0 =A0 =A0print self.getBizobj("contact")

> =A0 =A0 =A0 =A0dropDown.DataSource =3D "contact"

> =A0 =A0 =A0 =A0dropDown.DataField =3D "title_id"

> =A0 =A0 =A0 =A0print "choices", dropDown.Choices

> =A0 =A0 =A0 =A0print "keys", dropDown.Keys

>

>

> After typing the above I just re-read your original post, and see that

> the offending line is where you set dropDown.Value =3D vlKeys[0]. You

> generally don't want to do that, because the Value is coming from the

> bizobj and you are setting it to an arbitrary value which would get

> saved to the bizobj.

The dropDown.ValueMode =3D "key" has also been an offending line from

the beginning.

> However, you should be able to do that if you really want to, if the key

> is present in the choices. Otherwise, you'll get the error you

> originally reported. So, please still run with the above changes and

> let's see what the choices and keys are.

>

> Paul

>

The print is:

<biz.ContactBizobj.ContactBizobj (baseclass dabo.biz.dBizobj, id:439330288)=

>

choices [u'Sr.', u'Sr.\xaa', u'Arq.', u'Arq.\xaa', u'Eng.',

u'Eng.\xaa', u'Dr.', u'Dr.\xaa']

keys [25, 24, 23, 22, 21, 20, 19, 18]

The traceback is the same as before:

Traceback (most recent call last):

File "//usr/local/lib/wxPython-unicode-2.8.9.2/lib/python2.5/site-package=

s/wx-2.8-mac-unicode/wx/_misc.py",

line 1342, in Notify

self.notify()

File "//usr/local/lib/wxPython-unicode-2.8.9.2/lib/python2.5/site-package=

s/wx-2.8-mac-unicode/wx/_core.py",

line 14676, in Notify

self.result =3D self.callable(*self.args, **self.kwargs)

File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/sit=

e-packages/dabo/ui/uiwx/dForm.py",

line 123, in __update

super(BaseForm, self).update()

File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/sit=

e-packages/dabo/ui/uiwx/dPemMixin.py",

line 1244, in update

self.raiseEvent(dEvents.Update)

File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/sit=

e-packages/dabo/ui/uiwx/dPemMixin.py",

line 949, in raiseEvent

super(dPemMixin, self).raiseEvent(eventClass, nativeEvent, *args, **kwa=

rgs)

File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/sit=

e-packages/dabo/lib/eventMixin.py",

line 93, in raiseEvent

bindingFunction(event)

File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/sit=

e-packages/dabo/ui/uiwx/dPemMixin.py",

line 1225, in __onUpdate

self.update()

File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/sit=

e-packages/dabo/ui/uiwx/dPemMixin.py",

line 1244, in update

self.raiseEvent(dEvents.Update)

File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/sit=

e-packages/dabo/ui/uiwx/dPemMixin.py",

line 949, in raiseEvent

super(dPemMixin, self).raiseEvent(eventClass, nativeEvent, *args, **kwa=

rgs)

File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/sit=

e-packages/dabo/lib/eventMixin.py",

line 93, in raiseEvent

bindingFunction(event)

File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/sit=

e-packages/dabo/ui/uiwx/dPemMixin.py",

line 1225, in __onUpdate

self.update()

File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/sit=

e-packages/dabo/ui/dDataControlMixinBase.py",

line 97, in update

self.__dataUpdate()

File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/sit=

e-packages/dabo/ui/dDataControlMixinBase.py",

line 114, in __dataUpdate

self.Value =3D src.getFieldVal(self.DataField)

File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/sit=

e-packages/dabo/ui/uiwx/dControlItemMixin.py",

line 380, in _setValue

self.KeyValue =3D value

File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/sit=

e-packages/dabo/ui/uiwx/dControlItemMixin.py",

line 239, in _setKeyValue

raise ValueError(_("Trying to set %s.Value to these invalid

selections: %s") % (self.Name, invalidSelections))

ValueError: Trying to set dDropdownList1.Value to these invalid selections:=

[0]

If we run your code without the line:

dropDown.ValueMode =3D "key"

Then there is no exception raise. But, as expected, the value of the

"title_id" field does not get recorded to the db, because it is a

string, not an integer. So there is something wrong with setting

ValueMode.

HTH,

Miguel

©2009 Miguel Lopes