Hello, I was reading about the "Links to referencing 
records<http://web2py.com/books/default/chapter/29/07>"  
part in the online guide, but was confused how I would deal with custom 
logic of inserting it to the database. The guide seems to already have the 
values in the database to pull from. 

If we take the person and dog database in the example, a person can own 
many dogs, but a dog can only have one owner. 

If we take the example one step further and say we're running an animal 
shelter, we would have a list of people and a list of dogs, but we don't 
have the connection between the two. If a person wants to adopt a dog, 
they'll use a form which will have the following elements:

Textfield for the person's name. 
A list of checkboxes with the dog's name.

Then upon successful form submission, the selected dogs are linked to a 
person.

I don't quite understand how the form is built using "form = 
SQLFORM(db.person)" in this scenario. 

Since SQLFORM builds its own html, how would web2py handle the dog object? 
By default, it will probably use a textfield because the dog has a String 
name, but what about the owner reference? Furthermore, the dog has to be 
treated as a boolean since we are simply asking if this dog should belong 
to the human listed on the form or not. I can't quite picture how 
"SQLFORM.widgets.boolean.widget" 
since a dog isn't a boolean value tied to a person. It's its own entity 
with a name and an owner.

The only way I can think of to solve this is to NOT use SQLFORM, but rather 
SQLFORM.factory(). Then I would use "{{=form.custom.begin/end}}" to 
manually format the form, create the Textfield for the person name, and 
generate the checkboxes in a for loop given the list of dog names. When the 
form passes basic validation with form.accepted, I would grab the person's 
id and update the entry for all dogs that are returned checked. 

Do I have the right idea or am I misunderstanding web2py design? Is there 
an easier way to handle one-to-many relationships through a form?

-- 

--- 
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.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to