Tuesday, 15 February 2011

python - WTForm: how do I tell Form to use a custom method for one particular attribute? -



python - WTForm: how do I tell Form to use a custom method for one particular attribute? -

i have next definitions:

class producetype(ndb.model): crop = ndb.stringproperty() variety = ndb.stringproperty() class orderentry(ndb.model): producetype = ndb.keyproperty(kind=producetype, required=true) quantity = ndb.integerproperty(required=true)

i created next wtform:

class orderentryform(form): quantity = integerfield('quantity', [validators.required(), validators.numberrange(min=1)]) # dynamically adding choices producetype = selectfield('produce', [validators.required()], choices=[])

this doing in tests:

def setup(self): self.pt1 = producetype(crop='choi', variety='bok') self.pt2 = producetype(crop='cress', variety='true') self.pt1.put() self.pt2.put() def test_order_entry(self): oe = orderentry(producetype=self.pt1.key, quantity=20) oef = orderentryform(obj=oe) oef.producetype.choices = [ (self.pt1.key.id(), self.pt1.name()), (self.pt2.key.id(), self.pt2.name()) ] print oef.producetype

and what's beingness printed:

<select id="producetype" name="producetype"><option value="1">choi - bok</option><option value="2">cress - true</option></select>

problem: trying render form users edit.

so if orderentry in database looks likes:

order_entry = { producetype: self.pt1.key, quantity: 5 }

i expect wtforms rendering like:

<select id="producetype" name="producetype"> <option value="1" selected>choi - bok</option> <option value="2">cress - true</option> </select>

however, right cannot wtform render selected properly. select beingness rendered empty.

how wtform render selected correctly?

note:

during form population, i.e. oef = orderentryform(obj=oe), if wtform tries access obj.producetype, give ndb.key. can see, first fellow member of oef.producetype.choices tuple of ndb.key.id() opposed ndb.key

python wtforms

No comments:

Post a Comment