Once again, stuck passing values

137 views
Skip to first unread message

Weemo

unread,
Jul 30, 2021, 5:15:32 PM7/30/21
to py4web
Ok. I have most of the logic, application going. I am not using grids all the times. This is what I struggle the most with py4web.

There is no real way to pass on a value to populate another table and be able to query both based on the relationship of these two.

I have a customer table and a locations table,  for the sake of simplicity, I will just add a few fields from it:

db.define_table ('customer',
Field ('company'),
Field('contactname'))

db.define_table('locations',
Field('customer', 'reference customer')
Field('address'))


If I have a form that gives me the customer information, that is company and contactname and I have a button that would be to add a location for that customer, how in the world do I pass that customer id to the other locations table? 

The code below, used to work but not anymore. And I have not changed anything other than upgrade py4web:


@authenticated('newlocation/<id:int>','customer_location.html')
def customerlocation(id=None):
    if id is None:
        redirect(URL('custindex'))
    customer = db.customer[id]
    db.locations.customer.default = customer
    form = Form(db.locations, csrf_session=session, formstyle=FormStyleBootstrap4)

    if form.accepted:

           redirect(URL('order', form.vars['id']))

    return dict(form=form)


I just cant wrap my head around as to how can anyone work with relational databases if there are relations in tables without any relation at all? or am I missing something? 

By accomplishing what I want above, as a simple example, I should be able to eventually create queries  such as list of customers and their locations etc etc etc.

The grid is able to create the relationship so why cant I?

I have browsed over and over the documentation. cant find the answer. and like I said, it used to work before, not it doesnt.


Thanks


Jim Steil

unread,
Jul 31, 2021, 9:56:59 AM7/31/21
to py4web
When you say it isn't working, what exactly isn't working?  Are you getting errors?

What does the code look like that is calling newlocation/<id>?

I would look at changing this line:

db.locations.customer.default = customer

to this:

db.locations.customer.default = id

Also, is your pydal up to date?  I recall there was a py4web update in the past few months that required a pydal update (not positive, but I think that was the case).

Finally, have you looked at the SouthBreeze sample I posted a couple weeks ago?


All the code is here ->  https://github.com/jpsteil/southbreeze

I think it is doing what you are looking for. 

You can go in to Customers and add types and notes.  Employees and Orders have the same scenario.

Granted, this is all using htmx, but that shouldn't be an issue.  It is still just calling simple py4web controllers to return html.

-Jim

Kevin Keller

unread,
Jul 31, 2021, 11:16:32 AM7/31/21
to Jim Steil, py4web
Maybe this is also about the classic problem of using one form for fields of 2 or more related tables?

This is what you can do in py4web:
This is about a form that collects information about a presentation and also about which speaker is speaker at that presentation .

First line inserts all fields from the form for the first table presentations and gets back the newly created id value of that newly created row.
Then we can use that new row id and pass it on as a form value for that second table for id. 
here we create a new normally with the new foreign key already obtained.

There is also a chapter about this technique is in the web2py book which uses the same approach. here:



image.png

if form.accepted:
        id = db.presoatslot.insert(**db.presoatslot._filter_fields(form.vars))
        form.vars['presentation']=id
        id = db.speakersatpreso.insert(**db.speakersatpreso._filter_fields(form.vars))

--
You received this message because you are subscribed to the Google Groups "py4web" group.
To unsubscribe from this group and stop receiving emails from it, send an email to py4web+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/py4web/1aa801cb-adcc-4ed9-a6ab-48c57e672641n%40googlegroups.com.

Weemo

unread,
Aug 3, 2021, 1:31:18 PM8/3/21
to py4web
This dit it. Thanks!!

Weemo

unread,
Aug 3, 2021, 1:38:53 PM8/3/21
to py4web
You are totally right. Is this a very common scenario in WebApps? if it is, couldnt there be a function that would take care of that?  I see in the examples get_user() which returns the id of the user logged in. Something similar would be handy and speed the process of relating tables. 
Reply all
Reply to author
Forward
0 new messages