Question

I am a python/Turbogears noob and I really need your help. I read all the tutorials i could find and believe to be relevant to my problem.

I need to create a method/function for the kw to get the keys as the fields extracted from the database and for them to be empty, and then after the form is submitted the keys to filled with the values from the form and then stored in the database.

The problem is that the data types are not correct according to the fields in the database. I keep getting this error: 'str' object has no attribute '_sa_instance_state'...

Please help!

database structure:

orders table:
id
client_id
notes
offer_id
delivery_place_id
etc

I should also mention that most of them are foreign keys assigned to other fields in other tables...

python code:

class OrderController(RestController):
model = m.Order


order_table = OrderTable(DBSession)
order_filler = OrderTableFiller(DBSession)
order_add_form = OrderAddForm(DBSession)
order_edit_form = OrderEditForm(DBSession)  
order_edit_filler = OrderEditFiller(DBSession)


@validate(order_add_form, error_handler = None)
@expose("maivic.templates.order.new")

def new(self, **kw):
    #assert len(kw['notes'])==0
    #flash ( 'Rainbow honey double rainbow')
    """aici trebuie creata o metoda pentru a vedea ce date avem inainte de form si
    dupa submit"""
    #tmpl_context.widget = self.order_add_form    
    if 'sprox_id' in kw:
        del kw['sprox_id']
    tmpl_context.widget = self.order_add_form
    #1 start afiseaza new dar nu face nimic
    if len(kw) > 0: #pt a nu aduga un order nul 
        """    
        for k in kw.keys():
            if not kw[k]:
                del kw[k]"""

        print kw
        """if 'client' in kw and not isinstance(kw['client'], list):
            kw['client'] = [kw['client']] """  
        order = m.Order(**kw)
        DBSession.add(order)
        flash('order added')

        #redirect('/order')
    else:
        print "empty form" 

  #end1
    return dict(page="new", value = kw) 
Was it helpful?

Solution

Look at the validators in tw2.sqla (https://github.com/toscawidgets/tw2.sqla/blob/develop/tw2/sqla/widgets.py), you should be able to use them as field validators on your form. They will ensure that the ids entered in the *_id fields get converted to their corresponding model object (or raise a validation error if that doesn't exist).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top