Select fields?

14 views
Skip to first unread message

Voltron

unread,
Feb 28, 2012, 10:42:11 AM2/28/12
to Humongolus
Hi Christopher, could you post an example using:

1. Select fields
2. Multiple choice check buttons

Thanks

Christopher S Coté

unread,
Feb 28, 2012, 1:08:18 PM2/28/12
to humon...@googlegroups.com
Here's the different types of Choice fields. ModelChoice, Choice and
CollectionChoice

I had to make one little change to the field module, so you'll want to
update first before running this.

I'll work up some multi-selects and multi-checkboxes buttons. later
today or tomorrow.

from pymongo.connection import Connection
import logging
import humongolus as orm
import datetime
import humongolus.field as field
import humongolus.widget as widget
from tests.states import states

conn = Connection()
FORMAT = '%(asctime)-15s %(message)s'
logging.basicConfig(format=FORMAT)
logger = logging.getLogger("humongolus")

orm.settings(logger=logger, db_connection=conn)


states = {
'AL':"Alabama",
'AK':"Alaska",
'AZ':"Arizona",
'AR':"Arkansas",
'CA':"California",
'CO':"Colorado",
'CT':"Connecticut",
'DE':"Delaware",
'DC':"District Of Columbia",
'FL':"Florida",
'GA':"Georgia",
'HI':"Hawaii",
'ID':"Idaho",
'IL':"Illinois",
'IN':"Indiana",
'IA':"Iowa",
'KS':"Kansas",
'KY':"Kentucky",
'LA':"Louisiana",
'ME':"Maine",
'MD':"Maryland",
'MA':"Massachusetts",
'MI':"Michigan",
'MN':"Minnesota",
'MS':"Mississippi",
'MO':"Missouri",
'MT':"Montana",
'NE':"Nebraska",
'NV':"Nevada",
'NH':"New Hampshire",
'NJ':"New Jersey",
'NM':"New Mexico",
'NY':"New York",
'NC':"North Carolina",
'ND':"North Dakota",
'OH':"Ohio",
'OK':"Oklahoma",
'OR':"Oregon",
'PA':"Pennsylvania",
'RI':"Rhode Island",
'SC':"South Carolina",
'SD':"South Dakota",
'TN':"Tennessee",
'TX':"Texas",
'UT':"Utah",
'VT':"Vermont",
'VA':"Virginia",
'WA':"Washington",
'WV':"West Virginia",
'WI':"Wisconsin",
'WY':"Wyoming"
}

conn['test']['states'].remove()
for k,v in states.iteritems():
conn['test']['states'].insert({"abbrv":k, 'fullname':v})


def car_disp(car):
return {"value":car._id, "display":"%s %s %s" % (car.make,
car.model, car.year)}

def coll_display(doc):
return {'value':doc.get('abbrv'), 'display':doc.get('fullname', None)}

class Car(orm.Document):
_db = "test"
_collection = "cars"
owner = field.DynamicDocument()
make = field.Char()
model = field.Char()
year = field.Date()
silly_date = field.TimeStamp()

car = Car()
car.make = "Isuzu"
car.model = "Rodeo"
car.year = datetime.datetime(1998, 1, 1)
print car
c_id = car.save()

car2 = Car()
car2.make = "Mercedes"
car2.model = "Baby C"
car2.year = datetime.datetime(1965, 1, 1)
print car2
c_id = car2.save()

class Human(orm.Document):
_db = "test"
_collection = "humans"
car = field.ModelChoice(type=Car, widget=widget.Select,
render=car_disp)
color = field.Choice(choices=[{'value':'red',
'display':'Red'},{'value':'blue', 'display':'Blue'},{'value':'green',
'display':'Green'}])
state = field.CollectionChoice(db='test', collection='states',
render=coll_display, sort=[('fullname',1)])

class SelectorForm(widget.Form):
_fields = ['car', 'color', 'state']
car = widget.FormField(display="Car", widget=widget.Select)
color = widget.FormField(display="Color", widget=widget.Select)
state = widget.FormField(display="State", widget=widget.Select)

obj = Human()

a = SelectorForm(obj=obj)
print a.render()

Voltron

unread,
Feb 28, 2012, 1:49:35 PM2/28/12
to Humongolus
Thanks. Two other things:

1. I just noticed that I cannot render an empty form this way:

form = RegistrationForm()

It is to be rendered in the request handler that takes care of the
calls using the GET method,

2. Why does the obj passed to a form have to match exactly? I passed
an document object with slightly different attributes to a form, it
rendered a traceback.

Christopher S Coté

unread,
Feb 28, 2012, 1:54:11 PM2/28/12
to humon...@googlegroups.com
Yeah, it needs the fields from the object in order to render correctly,
I'll look into making the object optional.

Christopher S Coté

unread,
Feb 28, 2012, 8:14:14 PM2/28/12
to humon...@googlegroups.com
so..... Form and Widget support got a pretty big change today.

I would check out test.py and tests.test_field.Widget to see what it's
all about.

basically all widget functionality is done through the Forms now, no
more passing widgets to fields and rather than using FormField you just
use the actual widget.

It's much cleaner/less code, and separates the display stuff from your
models.

Should also be able to use a form without an object!


On 02/28/2012 12:49 PM, Voltron wrote:

Reply all
Reply to author
Forward
0 new messages