That solves it. Thanks! 

 - Franco

-----Original Message-----
From: django-users@googlegroups.com
[mailto:[EMAIL PROTECTED] On Behalf Of Malcolm Tredinnick
Sent: Saturday, May 19, 2007 12:17 PM
To: django-users@googlegroups.com
Subject: Re: Django newcomer - model question


On Sat, 2007-05-19 at 12:02 -0700, [EMAIL PROTECTED] wrote:
> 
>   Greetings. I'm trying to get my head wrapped around django, and the 
> ORM model. So far, I've been pleased with what I've experienced.
> However, I'm having some trouble nailing down a couple relations.
> 
> A container can hold (reference) multiple items of the same primary 
> key.  I need a method of storage in the ORM so i can effectively say, 
> "that container can hold 5 of those items", for example.
> 
> CREATE TABLE items
> (
>   id SERIAL NOT NULL UNIQUE,
>   PRIMARY KEY (id)
> );
> 
> CREATE TABLE containers
> (
>   id SERIAL NOT NULL UNIQUE,
>   PRIMARY KEY (id)
> };
> 
> CREATE TABLE containers_to_items
> (
>   container_id INTEGER NOT NULL REFERENCES containers (id),
>   item_id INTEGER NOT NULL REFERENCES items (id),
>   item_count INTEGER NOT NULL,
> );
> 
> My attempt at creating a model design is as follows:
> 
> class Item (models.Model):
>   id = models.AutoField(primary_key=True)
> 
> class Container (models.Model):
>   id = models.AutoField(primary_key=True)
>   items = models.ManyToManyField(Item)
> 
> However, this creates the following relation/join table:
> 
> CREATE TABLE whatever_django_labels_it (
>   container_id INTEGER NOT NULL REFERENCES containers (id),
>   item_id INTEGER NOT NULL REFERENCES items (id),
>   UNIQUE (container_id, item_id)
> );
> 
> So i need to find out how to model the original relation table field 
> called item_count, to represent that a container can hold multiple 
> items, or I need to be able to drop the django-imposed UNIQUE 
> constraint so I can add multiple identical item_id values for a single

> container_id value:
> 
> INSERT INTO whatever_django_labels_it (container_id, item_id) VALUES 
> (1, 1); INSERT INTO whatever_django_labels_it (container_id, item_id) 
> VALUES (1, 1); INSERT INTO whatever_django_labels_it (container_id, 
> item_id) VALUES (1, 1);
> 
>   etc
> 
> Input?

Any time when you want to put extra fields on the intermediary table of
a many-to-many relationship, you need to model that table explicitly and
use a couple of ForeignKey fields to set up the relationship.

See http://www.djangoproject.com/documentation/models/m2m_intermediary/
for an example.

Regards,
Malcolm





--~--~---------~--~----~------------~-------~--~----~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to