Electronics & Programming

develissimo

Open Source electronics development and programming

  • You are not logged in.

#1 Nov. 18, 2005 12:16:52

[EMAIL P.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Bi-Directional ManyToMany


Hi

I've just defined a manytomany relationship in the following way

class ItemType(meta.Model):
name = meta.CharField(maxlength=100)
descritpion = meta.CharField(maxlength=250)

class PropertyType(meta.Model):
name = meta.CharField(maxlength=100)
itemtypes = meta.ManyToManyField(ItemType)

Excellent. When I make a new property type in the admin screens I get a
mutiselect window for item types.

What I want to be able to do however is have this work back the other
way too so that whe I create a new item i can specify what property
types apply.

Any ideas on whether this is at all possible? Or how I might go about
achieving it

Maybe this kind of thinking is breaking thew rules but it does seem
logical that themany-to-many relationship be bi-directional in this way

Thanks
Charlie

Offline

#2 Nov. 18, 2005 12:18:01

[EMAIL P.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Bi-Directional ManyToMany


Perhaps I should have stated I'm using the new-admin branch

Offline

#3 Nov. 18, 2005 15:49:34

Robert W.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Bi-Directional ManyToMany


wrote:
> Hi
>
> I've just defined a manytomany relationship in the following way
>
> class ItemType(meta.Model):
> name = meta.CharField(maxlength=100)
> descritpion = meta.CharField(maxlength=250)
>
> class PropertyType(meta.Model):
> name = meta.CharField(maxlength=100)
> itemtypes = meta.ManyToManyField(ItemType)
>
> Excellent. When I make a new property type in the admin screens I get a
> mutiselect window for item types.
>
> What I want to be able to do however is have this work back the other
> way too so that whe I create a new item i can specify what property
> types apply.
>
> Any ideas on whether this is at all possible? Or how I might go about
> achieving it
>
> Maybe this kind of thinking is breaking thew rules but it does seem
> logical that themany-to-many relationship be bi-directional in this way
>
> Thanks
> Charlie
>
>

This wouldn't work yet but it also wouldn't be incredibly hard to add (
to new-admin).
The relevant places to look at would be

class Options in django/core/meta/__init__.py
get_data_holders:
add in get_all_related_many_to_many_objects()
get_all_related_many_to_many_objects:
make it return a subclass of RelatedObject rather than
RelatedObject, eg ManyToManyRelatedObject

In the new class ManyToManyRelatedObject,
override get_manipulator_fields, get_follow (make it false by
default), flatten_data, extract_data, maybe bind.

Then in your view, use follow = { "itemtypes" : True} .

Optionally:
Push down all the normal RelatedObject stuff into a subclass called
ForeignKeyRelatedObject and make Options.get_all_related_objects use
that. Only leave common behaviour in RelatedObject.
Define a new constructor kwarg for ManyToManyField to make them
followed both ways by default (ie in the admin). This would entail
changing ManyToManyRelatedObject.get_follow again as well.

Give it a go and/or file a ticket.

Offline

#4 Nov. 23, 2005 13:25:17

p.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Bi-Directional ManyToMany


Anyone working on this ? Would be great to have such functionality, so
if noone started to do it i could give a look. Also where will this
bidirectional ManyToMany select appear in itemtypes, below all the
controls it has in model ?

Offline

#5 Nov. 23, 2005 13:50:17

Robert W.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Bi-Directional ManyToMany


plisk wrote:
> Anyone working on this ? Would be great to have such functionality, so
> if noone started to do it i could give a look. Also where will this
> bidirectional ManyToMany select appear in itemtypes, below all the
> controls it has in model ?
>
>

I am working on core-fields removal atm in a private svk branch.
(Working on top of the mutually referential model changes). This changes
the location of quite a lot of the relevant code ( gets moved into fields).

I want to stick this stuff in a public branch when new-admin merges (
ask adrian_h ) as it is based off of new-admin currently, and having
branches off of branches doesn't seem like something we want to do right
now in the public repo.

So it would be fine to do it now, but it would be subject to a bit of
code churn. I'd prefer it not be done twice (specifically, I wouldn't
want to forward port your code myself...) , but if you are ok with
modifying it later for the other changes, go ahead.

I assume it would appear with the other related objects in the admin,
maybe just above edit-inline stuff.

Offline

#6 Nov. 23, 2005 14:10:06

p.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Bi-Directional ManyToMany


Its not that needs to be done asap, i'll wait until new-admin merges
and then give it a try.

Offline

Board footer

Moderator control

Enjoy the 24th of April
PoweredBy

The Forums are managed by develissimo stuff members, if you find any issues or misplaced content please help us to fix it. Thank you! Tell us via Contact Options
Leave a Message
Welcome to Develissimo Live Support