On Wed, Jun 27, 2012 at 8:57 AM, David Wagner <cptnwi...@gmail.com> wrote:
> With that said, I'm having trouble wrapping my brain around the ManyToMany
> and Many-To-One and OneToOne concepts. I'm trying to create a multi-select
> option for a user profile in which a user can have multiple attributes of a
> certain kind. Right now I'm just working on the model. This is what I have,
> is it right?

these terms are used mainly in the relational database design.  as you
might know, you can't have real arrays or structures on traditional
SQL databases, so you use those three types of 'links'.

the simplest is the 'many to one', you have one 'parent' table and a
'child' one with a field (called the "foreign key" field) where each
record keeps a copy of the primary key of a 'parent' record.  a common
example is to have 'group' and 'member' tables.  each 'member' record
has a field (sometimes called 'group_id') that tells which to which
group it belongs.  with a non-unique index on that field, it's easy to
pick all members of each group.

a slight variation is the 'one to one'.  here, the index on the
foreign key field is a 'unique' index, that forbids two records with
the same value, effectively making it impossible for a 'parent' record
to have more than one 'child'.  sometimes instead of defining a
specific foreign key field, you assign the same primary key value to
related records.

the last one is the 'many to many'.  for example, you can have users
that belong to more than one group; and of course each group has many
users.  for this, you have to define a 'link table' where each record
has two foreign key fields, one for each linked table.  sometimes the
primary key of the link table is just the composite index of these two
foreign keys.  also there can be a second composite index with the
same foreign keys in the opposite order.  Also you can choose to add
some extra fields to the link table to express some properties of the
link itself.

In Django, you handle this just by declaring fields of the appropriate
type; Django takes care of declaring the indexes, link table if
needed, and a 'reverse relationship'.  that last feature is the one
that makes the 'members' pseudo array to magically appear in 'group'
objects.

hope this helps with the theory and makes the Django docs easier to read.

-- 
Javier

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@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.

Reply via email to