[web2py] Help with project (many to many form or table)
Hello, I'm creating a small project to test the use of web2py. After going through some of the documentation for web2py I've found myself in quite a problem to deal with a many to many insert/update form. The simplified context for the example is this: Model: There are people (person) and each can only have a home (home). Homes (home) have many types of rooms (home_room). Types of rooms (room) are asigned to homes (home). A person can spend an amount of minutes in a specific room of his home (person_room). The table to record the amount of minutes a person spends in a room can have only one record for a person/room of the home. Controller: Index. Show a list of the people in the DB. Register minutes. Shows a list of the rooms available to the person according to the home and process the request to update amount of minutes for each room. View: Index. Displays a list of records for person. Each person links to the register url and using the args sends the person's id. Register minutes. Should display a table (or form) with the rooms and any previous minutes registered to a person so the amounts can be entered or updated. So my problem is creating a table (or form) that presents the left join for home_rooms and person so I can enter the minutes for each room and submit the information. I've been thinking a solution could be to insert first any records from the left join into the person_room table before creating the table (or form), but this still leaves me the problem of showing several records to update the amount of minutes. CODE: Model: db.define_table('room', Field('name', 'string')) db.define_table('home', Field('name', 'string')) db.define_table('home_room', Field('description', 'string'), Field('home_id', 'references home'), Field('room_id', 'references room')) db.define_table('person', Field('name', 'string'), Field('home_id','references home')) db.define_table('person_room', Field('minutes', 'integer'), Field('person_id', 'references person'), Field('home_room_id', 'references home_room')) Data: Home: home.id home.name 1 city 2 beach 3 winter Room: room.id room.name 1 principal 2 secundary 3 living 4 dining 5 gaming Home_Room: home_room.id home_room.description home_room.home_id home_room.room_id 1 city principal 1 1 2 city secondary 1 2 3 city living 1 3 4 beach principal 2 1 5 beach living 2 3 6 beach dining 2 4 7 winter principal 3 1 8 winter living 3 3 9 winter gaming 3 5 Person: person.id person.name person.home_id 1 David 1 2 Sara 2 3 Charles 3 4 Rose 1 5 Fred 2 6 Martin 3 Controllers (default): def index(): ppl = db().select(db.person.id, db.person.name) return dict(people=ppl) def register(): aForm = None aPerson = db(db.person.id==request.args(0, cast=int, otherwise=URL('index') ) ).select(db.person.id, db.person.name, ).first() or redirect(URL('index')) fields = ['person_id', 'home_room_id','minutes'] aForm = SQLFORM(db.person_room, fields=fields) aForm.vars.person_id = aPerson.id aForm.process(keepvalues=True) return dict(person=aPerson, form = aForm) Views: default/index {{extend 'layout.html'}} {{if len(people) == 0:}} There's no information {{else:}} {{for row in people:}} {{=A('%s'%row.name, _href=URL("register", args=row.id))}} {{pass}} {{pass}} default/register {{extend 'layout.html'}} {{=person.name}} {{=form or ''}} -- Resources: - http://web2py.com - http://web2py.com/book (Documentation) - http://github.com/web2py/web2py (Source code) - https://code.google.com/p/web2py/issues/list (Report Issues) --- You received this message because you are subscribed to the Google Groups "web2py-users" group. To unsubscribe from this group and stop receiving emails from it, send an email to web2py+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/web2py/8c397a93-79f8-43e5-8967-759ebb9611b2%40googlegroups.com.
[web2py] Re: Help with request.args() and SQLFORM.grid(). Error 404
Ok... So I found the solution and it was a TOTAL noob mistake. Turns out that SQLFORM.grid() messes up with the requests arguments and in the initial examples of the book this is never mentioned. In Chapter 7 of the book (english 6th edition) I found the answer: (http://web2py.com/books/default/chapter/29/07/forms-and-validators#Using-requests-args-safely) In order for the grid to work properly, I had to give it this parameter: *args=request.args[:1]* that way the Grid knows to preserve the first argument for the request. The code in the controller ended up working like this: @auth.requires_login() def testArg(): test = db.test(request.args(0,cast=int, otherwise=URL('index'))) or redirect(URL('index')) grid = SQLFORM.grid(db(db.test.id == 2), *args=request.args[:1]*, deletable=False, editable=True) return locals() On Wednesday, January 15, 2020 at 1:04:00 AM UTC-5, Juan Gutiérrez wrote: > > This sounds crazy but I'm getting an error 404 when using in the same > function this lines together: > > test = db.test(request.args(0,cast=int, otherwise=URL('index'))) or > redirect(URL('index')) > grid = SQLFORM.grid(db(db.test.id == 2), deletable=False, editable=True) > > When I comment either one of the lines, the function returns the correct > values for test and grid. When put together, it doesn't work and I get > redirected or a 404 error. > > the complete escenario is: > Request: > http://127.0.0.1/gk/default/testArg/1 > > Model: > db.define_table('test', > Field('nombre', 'string', unique=True, label='Nombre'), > Field('gerencia', label='Gerencia'), > Field('equipo', label='Equipo'), > Field('vigencia', 'date', label='Vigencia'), > format = '%(nombre)s' > ) > > Controler: > @auth.requires_login() > def testArg(): > test = db.test(request.args(0,cast=int, otherwise=URL('index'))) or > redirect(URL('index')) > grid = SQLFORM.grid(db(db.test.id == 2))#, deletable=False, > editable=True) > return locals() > > View: > generic.html > > Am I missing something really obvious? > > David. > -- Resources: - http://web2py.com - http://web2py.com/book (Documentation) - http://github.com/web2py/web2py (Source code) - https://code.google.com/p/web2py/issues/list (Report Issues) --- You received this message because you are subscribed to the Google Groups "web2py-users" group. To unsubscribe from this group and stop receiving emails from it, send an email to web2py+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/web2py/d08b6b21-6c71-4b2c-9f4d-18f049b9a4b7%40googlegroups.com.
[web2py] Help with request.args() and SQLFORM.grid(). Error 404
This sounds crazy but I'm getting an error 404 when using in the same function this lines together: test = db.test(request.args(0,cast=int, otherwise=URL('index'))) or redirect(URL('index')) grid = SQLFORM.grid(db(db.test.id == 2), deletable=False, editable=True) When I comment either one of the lines, the function returns the correct values for test and grid. When put together, it doesn't work and I get redirected or a 404 error. the complete escenario is: Request: http://127.0.0.1/gk/default/testArg/1 Model: db.define_table('test', Field('nombre', 'string', unique=True, label='Nombre'), Field('gerencia', label='Gerencia'), Field('equipo', label='Equipo'), Field('vigencia', 'date', label='Vigencia'), format = '%(nombre)s' ) Controler: @auth.requires_login() def testArg(): test = db.test(request.args(0,cast=int, otherwise=URL('index'))) or redirect(URL('index')) grid = SQLFORM.grid(db(db.test.id == 2))#, deletable=False, editable=True) return locals() View: generic.html Am I missing something really obvious? David. -- Resources: - http://web2py.com - http://web2py.com/book (Documentation) - http://github.com/web2py/web2py (Source code) - https://code.google.com/p/web2py/issues/list (Report Issues) --- You received this message because you are subscribed to the Google Groups "web2py-users" group. To unsubscribe from this group and stop receiving emails from it, send an email to web2py+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/web2py/8e05d374-8497-4818-b6e3-e84df61dc4be%40googlegroups.com.