On 4/18/07, C <[EMAIL PROTECTED]> wrote:
>
> Hi,
>
> I am building a simple parts database as a fun "learn django" project.
> A "part" in my test application has a part number, description, etc
> and can optionally be composed of other parts.  This sounded to me
> like a good candidate for a self referring many to many relationship.
>
> I got it coded and in the django admin, I can enter part number etc.
> and multi-select from a list of other "part"s.  The problem is that if
> I have a part that is composed of say an axle and two wheels (each of
> which are also a part), I don't have a way in the admin to specify
> that there are two wheels. I can only pick one, because only one
> exists.  How do I specify that there are two wheels?  Is it possible
> to do what I want with the many to many to self model?

I assume by this that you mean that there is only 1 actual wheel part
in the database, but you want to say that the 'back end of a car' part
contains 1 instance of the axle part and two instances of the wheel
part.

If this is the case, then no - you don't want a m2m to self. m2m
relations are about saying row X in the database is related to row Y
in the database; you can't add multiple instances of this
relationship.

What you need is this ability to define the composition, which can
imply multiple instances of a given relationship. There are probably a
number of ways to solve this problem; the 'right' approach will depend
a little bit on your specific requirements.

One possible solution is to define an m2m relationship with
intermediary data. To do this, you don't put the m2m definition
directly on the Part model - you define a new model that fakes what
the m2m definition does:

class Relation(Model):
    parent = ForeignKey(Part)
    child = ForeignKey(Part)
    number = IntegerField()

The instances of relation essentially do what an m2m field does - sets
up a table that connects the two models - but allows you to piggyback
data _about_ the relationship onto each connection. In this case, the
extra data is the number of the child part required to construct the
parent.

Yours,
Russ Magee %-)

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