Toby, The online Web2py manual explains how to set up many-to-many relationships.
If this is new to you, start reading about one-to-many relationships and continue from there. http://web2py.com/books/default/chapter/29/06#One-to-many-relation On Monday, February 25, 2013 2:24:48 AM UTC-5, Toby Wilson wrote: > > > Hi all, > > New to web development and web2py. Know a bit of Python. > > As a learning experience I’ve written a food tracking program that works > really well in Python and it just runs on my local PC. It just reads and > writes values into text files. > > Now I’m trying to transfer that into web2py as a next step learning > experience. I’m trying to figure out the database side of it (models). > > It seems what I’m dealing with is many-to-many relationships. I have a > list of meals and each meal has a list of associated ingredients. So one > ingredient can be in many meal and each meal has many ingredients. > > So essentially, what the program I've written can do is add 3 x “Meal 1” > to the quantity existing in the database for “Meal 1” and then after doing > that prompt the user with: > > “Do you want this to impact the level of the ingredient ‘carrot’?” Y/N > > And then: > > “Do you want this to impact the level of the ingredient ‘steak’? Y/N > > etc. > > And the program knows that “Meal 1” needs 3 carrots, so upon entering “Y” > to the first question, 3 carrots are automatically subtracted from the > stock level of carrots in the database. > > So as meals are added, the user is prompted with associated ingredients > and can choose to update or not update the level of that associated > ingredient. > > In the local Python program I’ve written, all of this is just done through > listing things in dictionaries and using functions to manipulate the data > and then read and write to text files. Now I want to use database tables > and I’m not sure how much of the association should be in the model and how > much (if any) should be in the controller (for example in the form of a > dictionary). > > I’ve read through the documentation on many to many relationships but > haven’t got a full grasp of it yet. > > I’ve coded a few different database structures but haven’t managed to get > it right yet. > > The way I've been thinking about it is, a table for meals, a table for > ingredients, and perhaps a table for associations? I've used "reference" to > get the drop down box associated, but I can't select more than ingredient. > I had a look at the Django admin login and it visually made this look a bit > easier - i.e. building the one to many relationship. How is this done in > web2py? > > Any help or advice would be much appreciated. > > Toby > > P.S. If I have a database file in web2py and open the file through the > web2py IDE and replace the contents with a totally different database > structure and save it, what happens? Does web2py just remove what was there > in the database and replace the database tables with the new ones or does > it manage it in a different way (i.e. only add new elements etc.)? > > > -- --- 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.