This is a fabulous response, Peter. Thank you very much for making this so clear. The samurai_set is a revelation for me as well. I see now that I should look more carefully at the methods available for the models.
If I can ask another question about this same topic, is there a way to store a list variable in a model as a field (without creating another table)? For example, I want to store a list of the exits for a province (one can only exit to a bordering province). Is there an SQL field that lets me do this so I can just do a "if province.has_exit() ..." check? Or do I need to cross-reference it to a table called Exits which has multiple exits associated with each province. There will only ever be 61 provinces and there is no province with more than 6 or 7 exits. With such a small set, I doubt there would be database efficiency reasons to use a table. So I'd prefer a list if possible. Cheers. -Tim On Thu, Feb 18, 2010 at 11:38 PM, Peter Herndon <tphern...@gmail.com> wrote: > > On Feb 18, 2010, at 11:47 PM, Timothy Kinney wrote: > > > > > Here's the system I'm currently using that doesn't work too well... > > > > Samurai (id, name) > > > > Room (id, name, ForeignKey(Samurai), ForeignKey(Province)) > > > > Province (id, name) > > > > > > Can someone suggest which relationships to use to get the results that > > I want and explain why the relationship helps to accomplish that? > > > > If a given samurai must be in a room, and a room can hold multiple samurai, > your models should instead be: > > Samurai (id, name, ForeignKey(Room)) > > Room (id, name, ForeignKey(Province)) > > Province (id, name) > > With this set of models, a single Samurai has an FK reference to the room > in which he finds himself. The Room, very similarly, has a reference to the > Province in which it is located. A Province can hold many Rooms, and a Room > can hold many Samurai. In Django's ORM models, a given instance of the Room > model (e.g., "room = Room.objects.get(pk=1)") will have a method that allows > access to all Samurais residing in that Room. So, "room.samurai_set" will > give you the Room's Samurais. Similarly, for a given province, you can > retrieve all rooms via "province.room_set". Relevant Django docs are here: > > http://docs.djangoproject.com/en/1.1/ref/models/relations/#ref-models-relations > > In a view, assuming you are given a Samurai, you can get to the room name > via samurai.room.name, and the province name by samurai.room.province.name. > If, in your view, you pass the Samurai object to the template, your > template can perform that same dereference and thus you can present to the > user the room name and province name in your HTML. Relevant Django docs > here: http://docs.djangoproject.com/en/1.1/topics/templates/#variables > > For the canonical book on relational database theory, I'd suggest > "Introduction to Database Systems", by Chris Date. Not for the faint of > heart, the math-challenged, the practical or those short of time, but it > will explain relational theory in detail. Great detail. Ye be warned. ;) > > I hope this helps, though the Date book probably won't, at least not > without long and sustained study. There are likely other books on RDBMSs > that folks can recommend. Other than that, my apologies, it's late and I no > longer have a functioning brain. > > ---Peter Herndon > > -- > You received this message because you are subscribed to the Google Groups > "Django users" group. > To post to this group, send email to django-us...@googlegroups.com. > To unsubscribe from this group, send email to > django-users+unsubscr...@googlegroups.com<django-users%2bunsubscr...@googlegroups.com> > . > For more options, visit this group at > http://groups.google.com/group/django-users?hl=en. > > -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.