[Cubicweb] On-the-fly modification of forms

St├ęphane Bugat stephane.bugat at free.fr
Wed Aug 22 14:42:32 CEST 2012


 > Weird. Much probably a selection pb. Could you send us code for your
> class hierarchy + __select__?

Of course I can:

def get_airports(form, field):
    execute = form._cw.execute
    return [{'value': eid, 'label': name} 
            for eid, name in execute(u'DISTINCT Any X,N ORDERBY N WHERE '
                u'X name N, X is Airport')]

class MyAirportSearchFormView(EditionFormView):
    __select__ = EditionFormView.__select__ & is_instance('AirportSearch')

    def init_form(self, form, entity):
        oneway = ff.BooleanField(
                name=_('search_type'),
                label=_('Search type: '),
                choices=(u'single airport', u'trip'),
        )
        form.insert_field_before(oneway, u'airport_a_eid', 'subject')
        airport_a_field = form.field_by_name(u'airport_a_eid', 'subject')
        airport_a_field.widget = fw.LazyRestrictedAutoCompletionWidget(
                autocomplete_initfunc=get_airports)

class AirportSearchCreationFormView(MyAirportSearchFormView):
    __select__ = CreationFormView.__select__ & specified_etype_implements('AirportSearch')


> > As mentioned, the boolean field is not shown in the creation form.
> > However, I did create an AirportSearch object and tried to modify
> > it afterwards, but I just got an error: "TypeError: sequence item
> > 1: expected string or Unicode, NoneType found" for
> > "/usr/lib/python2.6/dist-packages/cubicweb/web/views/forms.py,
> > line 244, function build_context".
> > 
> > Any idea about that?
> 
> Yep. That's because when creating a field then adding it to a form as
> you're
> doing it, you must provide name='something' to the field's
> constructor. I'll
> add an assertion for this trying to make it clearer.

Works fine for modification forms since it was adding... Great!



More information about the Cubicweb mailing list