Hi Ralph,
That approach looked good in theory, but still gives me trouble:
"TypeError: unbound method fetch_data() must be called with UserForm_d
instance as first argument (got Request instance instead)"
I had a look at the tw2.sqla.DbFormPage class, & fetch_data looks like this:
""" A page that contains a form with database synchronisation. The `fetch_data` method loads a record from the database, based on the primary key in the URL (no parameters for a new record). The `validated_request` method saves the data to the database. """ def fetch_data(self, req): self.value = req.GET and self.entity.query.filter_by(**req.GET.mixed()).first() or None
The
first problem here is that DbFormPage.fetch_data() is expecting a
particular type of URL - one with a GET value, eg /user/edit?id=1. I am
using a different approach to formatting a URL, eg /user/1/edit, &
with Pyramid & URL dispatch, you would access those parameters via
the request object itself eg: id = request.matchdict.get('id', None). In
fact, my request.GET.mixed() is empty. So this implementation of
fetch_data does make the assumption that you are formatting your URL
parameters in a particular way, & it fails if you use any other
approach.
That said, even if I craft my URL params the way it
expects, I still get the above error, so I assume I am still missing
something. "Unbound method" suggests a need to bind things somehow, but
I'm not sure how that should be done at this stage...
I have to
also say that while it is handy to have fetch_data automagically get my
data, I would feel more comfortable either passing a complete query
string or a query result object to the form, so I can have control over
the queries in my application, rather than pass that on to TW2. I
understand that for many people this is handy "magic", but it's not what
I am trying to achieve...
However, I did finally manage to work
out how to do this in Pyramid. Rather than fetch_data, which sets the
value of self.value, I just set it directly in the view:
@view_config(route_name='user_edit', renderer='my_mako_template.html')
def user_edit_view(request):
id = request.matchdict.get('id', None)
dbsession = DBSession()
form = UserForm()
user = dbsession.query(User).filter(User.id==id).first()
form.value = user
return {'form':form}
So hopefully that helps anyone else trying to achieve the same thing...
Cheers,
Bruce