Re: [Django] #897: Bi-Directional ManyToMany in Admin

47 views
Skip to first unread message

Django

unread,
May 18, 2011, 10:04:50 AM5/18/11
to django-...@googlegroups.com
#897: Bi-Directional ManyToMany in Admin
---------------------------------------+-------------------------------
Reporter: anonymous | Owner: nobody
Type: New feature | Status: new
Milestone: | Component: contrib.admin
Version: | Severity: Normal
Resolution: | Keywords:
Triage Stage: Accepted | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
---------------------------------------+-------------------------------
Changes (by CarstenF):

* cc: carsten.fuchs@… (added)
* easy: => 0


--
Ticket URL: <https://code.djangoproject.com/ticket/897#comment:26>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Jun 14, 2011, 5:35:37 PM6/14/11
to django-...@googlegroups.com
#897: Bi-Directional ManyToMany in Admin
---------------------------------------+-------------------------------
Reporter: anonymous | Owner: nobody
Type: New feature | Status: new
Milestone: | Component: contrib.admin
Version: | Severity: Normal
Resolution: | Keywords:
Triage Stage: Accepted | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
---------------------------------------+-------------------------------
Changes (by croepha):

* ui_ux: => 0


Comment:

etienned's solution did not work for me, I am doing this instead:

{{{

class User(models.Model):
groups = models.ManyToManyField('Group',
db_table='testapp_user_groups')

class Group(models.Model):
users = models.ManyToManyField('User',
db_table=User.groups.field.db_table)
Group.users.through._meta.managed = False
}}}


This will also work for non symmetrical self M2M relationships:
{{{

class User(models.Model):
supervisors = models.ManyToManyField('self',
related_name='underlings_set', db_table='testapp_user_supervisors')
underlings = models.ManyToManyField('self',
related_name='supervisors_set', db_table=supervisors.db_table)
underlings._get_m2m_attr = supervisors._get_m2m_reverse_attr
underlings._get_m2m_reverse_attr = supervisors._get_m2m_attr
User.underlings.through._meta.managed = False
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/897#comment:27>

Django

unread,
Jul 6, 2011, 3:24:32 AM7/6/11
to django-...@googlegroups.com
#897: Bi-Directional ManyToMany in Admin
---------------------------------------+-------------------------------
Reporter: anonymous | Owner: nobody
Type: New feature | Status: new
Milestone: | Component: contrib.admin
Version: | Severity: Normal
Resolution: | Keywords:
Triage Stage: Accepted | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
---------------------------------------+-------------------------------

Comment (by anonymous):

Check this out

{{{
class Test1(models.Model):
tests2 = models.ManyToManyField('Test2', blank=True)

class Test2(models.Model):
tests1 = models.ManyToManyField(Test1, through=Test1.tests2.through,
blank=True)
}}}

I guess this is the most native way

--
Ticket URL: <https://code.djangoproject.com/ticket/897#comment:28>

Django

unread,
Sep 28, 2012, 1:36:59 PM9/28/12
to django-...@googlegroups.com
#897: Bi-Directional ManyToMany in Admin
-------------------------------+------------------------------------

Reporter: anonymous | Owner: nobody
Type: New feature | Status: new
Component: contrib.admin | Version:
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+------------------------------------

Comment (by CB):

To update comment 28:

This works great, but breaks south. So here's a fix for that:
{{{
class ReverseManyToManyField(models.ManyToManyField):
pass
try:
import south
except ImportError:
pass
else:
from south.modelsinspector import add_ignored_fields
add_ignored_fields([".*\.ReverseManyToManyField$",])

class Test1(models.Model):
tests2 = models.ManyToManyField('Test2', blank=True)

class Test2(models.Model):
tests1 = models.ReverseManyToManyField(Test1,
through=Test1.tests2.through, blank=True)
}}}

A possible Django enchancement would be adding this field (or rather, the
results of it's contribute_to_class, I think) instead of the
ReverseManyRelatedObjectsDescriptor

--
Ticket URL: <https://code.djangoproject.com/ticket/897#comment:29>

Django

unread,
Apr 28, 2013, 4:02:53 AM4/28/13
to django-...@googlegroups.com
#897: Bi-Directional ManyToMany in Admin
-------------------------------+------------------------------------
Reporter: anonymous | Owner: nobody
Type: New feature | Status: new
Component: contrib.admin | Version:
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+------------------------------------

Comment (by rhunwicks):

Using Django 1.5.1 I was getting:
{{{
app.test1: Reverse query name for m2m field 'tests2' clashes with m2m
field 'Test2.tests1'. Add a related_name argument to the definition for
'tests2'
}}}

This is almost certainly because my field and model name is the same.
Either way, I have solved it by adding `related_name` to `Test1.tests2`
and taking the opportunity to suppress it from `Test2`:
{{{
class Test1(models.Model):
tests2 = models.ManyToManyField('Test2', related_name='test2_set+',
blank=True)

class Test2(models.Model):
tests1 = models.ReverseManyToManyField(Test1,
through=Test1.tests2.through, blank=True)
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/897#comment:30>

Django

unread,
Jul 10, 2013, 11:40:47 AM7/10/13
to django-...@googlegroups.com
#897: Bi-Directional ManyToMany in Admin
-------------------------------+------------------------------------
Reporter: anonymous | Owner: nobody
Type: New feature | Status: new
Component: contrib.admin | Version:
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+------------------------------------

Comment (by kux):

For people who still bump into this, it might be worth checking
https://github.com/kux/django-admin-extend

It provides a mechanism for injecting bidirectional many-to-many fields in
ModelAdmins that have already been defined by other apps.

--
Ticket URL: <https://code.djangoproject.com/ticket/897#comment:31>

Django

unread,
Sep 4, 2013, 5:36:09 AM9/4/13
to django-...@googlegroups.com
#897: Bi-Directional ManyToMany in Admin
-------------------------------+------------------------------------
Reporter: anonymous | Owner: nobody
Type: New feature | Status: new
Component: contrib.admin | Version:
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+------------------------------------

Comment (by Koen Biermans <koen@…>):

This seems to be more or less the same as #10964. I have a more generic
solution for it on that ticket that allows you to use the reverse
descriptor for M2M or nullable FK (like 'book_set') in the fields set for
any modelform. It is also available in admin (also for filter_horizontal
and filter_vertical).

Should one of these be considered as a duplicate?

--
Ticket URL: <https://code.djangoproject.com/ticket/897#comment:32>

Django

unread,
Jul 30, 2014, 12:40:43 PM7/30/14
to django-...@googlegroups.com
#897: Bi-Directional ManyToMany in Admin
-------------------------------+------------------------------------
Reporter: anonymous | Owner: nobody
Type: New feature | Status: new
Component: contrib.admin | Version:
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+------------------------------------
Changes (by CollinAnderson):

* cc: cmawebsite@… (added)


Comment:

I started trying to get the patches from #10964 applying cleanly (and
correctly) to master, but I think it's worth waiting for the new _meta
options API. https://github.com/django/django/pull/2894

--
Ticket URL: <https://code.djangoproject.com/ticket/897#comment:33>

Django

unread,
Jan 15, 2015, 4:06:46 PM1/15/15
to django-...@googlegroups.com
#897: Bi-Directional ManyToMany in Admin
-------------------------------+------------------------------------
Reporter: anonymous | Owner: nobody
Type: New feature | Status: new
Component: contrib.admin | Version:
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+------------------------------------

Comment (by jschneier):

The new _meta options api has landed. I'm very interested in seeing this
make it across the finish line and would be happy to do the bulk of the
work with a little bit of direction. (Not sure where the best place to
post that kind of query).

--
Ticket URL: <https://code.djangoproject.com/ticket/897#comment:34>

Django

unread,
Jan 15, 2015, 5:09:34 PM1/15/15
to django-...@googlegroups.com
#897: Bi-Directional ManyToMany in Admin
-------------------------------+------------------------------------
Reporter: anonymous | Owner: nobody
Type: New feature | Status: new
Component: contrib.admin | Version:
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+------------------------------------

Comment (by collinanderson):

Awesome. As some have mentioned, I think the next basic step make auto
created reverse related fields (especially ManyToManyRel) into actual
fields.

After that, we'd need to allow ManyToManyRel to be used in any model form.

Here's what I think would be a start. (Doesn't work at all)
https://github.com/django/django/pull/3927/files

--
Ticket URL: <https://code.djangoproject.com/ticket/897#comment:35>

Django

unread,
Feb 19, 2015, 12:51:05 PM2/19/15
to django-...@googlegroups.com
#897: Bi-Directional ManyToMany in Admin
-------------------------------+------------------------------------
Reporter: anonymous | Owner: nobody
Type: New feature | Status: new
Component: contrib.admin | Version:
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+------------------------------------

Comment (by collinanderson):

#24317 should fix this.

--
Ticket URL: <https://code.djangoproject.com/ticket/897#comment:36>

Django

unread,
Oct 1, 2015, 4:48:36 AM10/1/15
to django-...@googlegroups.com
#897: Bi-Directional ManyToMany in Admin
-------------------------------+------------------------------------
Reporter: anonymous | Owner: nobody
Type: New feature | Status: new
Component: contrib.admin | Version:
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+------------------------------------

Comment (by auvipy):

Replying to [comment:36 collinanderson]:
> #24317 should fix this.

then the issue should be closed?

--
Ticket URL: <https://code.djangoproject.com/ticket/897#comment:37>

Django

unread,
Oct 1, 2015, 12:09:14 PM10/1/15
to django-...@googlegroups.com
#897: Bi-Directional ManyToMany in Admin
-------------------------------+------------------------------------
Reporter: anonymous | Owner: nobody
Type: New feature | Status: new
Component: contrib.admin | Version:
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+------------------------------------

Comment (by claudep):

Note the "should". I'd rather see the result of #24317 once committed
before closing this one.

--
Ticket URL: <https://code.djangoproject.com/ticket/897#comment:38>

Django

unread,
Jan 5, 2016, 4:46:12 PM1/5/16
to django-...@googlegroups.com
#897: Bi-Directional ManyToMany in Admin
-------------------------------+------------------------------------
Reporter: anonymous | Owner: auvipy
Type: New feature | Status: assigned
Component: contrib.admin | Version: master

Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+------------------------------------
Changes (by auvipy):

* status: new => assigned
* owner: nobody => auvipy
* version: => master


--
Ticket URL: <https://code.djangoproject.com/ticket/897#comment:39>

Django

unread,
Jan 6, 2016, 12:05:11 PM1/6/16
to django-...@googlegroups.com
#897: Bi-Directional ManyToMany in Admin
-------------------------------+------------------------------------
Reporter: anonymous | Owner:
Type: New feature | Status: new
Component: contrib.admin | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+------------------------------------
Changes (by auvipy):

* owner: auvipy =>
* status: assigned => new


--
Ticket URL: <https://code.djangoproject.com/ticket/897#comment:40>

Django

unread,
Mar 1, 2016, 8:54:35 PM3/1/16
to django-...@googlegroups.com
#897: Bi-Directional ManyToMany in Admin
-------------------------------+------------------------------------
Reporter: anonymous | Owner:
Type: New feature | Status: new
Component: contrib.admin | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+------------------------------------

Comment (by Chronial):

This snippet provides a workaround for this: https://snipt.net/chrisdpratt
/symmetrical-manytomany-filter-horizontal-in-django-admin/
(and does it at the form level instead of with models)

--
Ticket URL: <https://code.djangoproject.com/ticket/897#comment:41>

Django

unread,
Mar 11, 2016, 11:10:11 PM3/11/16
to django-...@googlegroups.com
#897: Bi-Directional ManyToMany in Admin
-------------------------------+------------------------------------
Reporter: anonymous | Owner:
Type: New feature | Status: new
Component: contrib.admin | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+------------------------------------
Changes (by mitar):

* cc: mmitar@… (added)


--
Ticket URL: <https://code.djangoproject.com/ticket/897#comment:42>

Django

unread,
Mar 12, 2016, 1:16:58 AM3/12/16
to django-...@googlegroups.com
#897: Bi-Directional ManyToMany in Admin
-------------------------------+------------------------------------
Reporter: anonymous | Owner:
Type: New feature | Status: new
Component: contrib.admin | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+------------------------------------

Comment (by mitar):

The form approach does not work correctly because in the history view of
the model in admin there are no entries for changed reverse M2M fields.

--
Ticket URL: <https://code.djangoproject.com/ticket/897#comment:43>

Django

unread,
Sep 13, 2018, 3:28:45 PM9/13/18
to django-...@googlegroups.com
#897: Bi-Directional ManyToMany in Admin
-------------------------------+------------------------------------
Reporter: anonymous | Owner: nobody

Type: New feature | Status: new
Component: contrib.admin | Version:
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+------------------------------------

Comment (by Ramiro Morales):

Isn't this use case covered by this admin functionality?:
https://docs.djangoproject.com/en/2.1/ref/contrib/admin/#working-with-
many-to-many-models

--
Ticket URL: <https://code.djangoproject.com/ticket/897#comment:44>

Django

unread,
Sep 14, 2018, 10:49:56 AM9/14/18
to django-...@googlegroups.com
#897: Bi-Directional ManyToMany in Admin
-------------------------------+------------------------------------
Reporter: anonymous | Owner: nobody
Type: New feature | Status: new
Component: contrib.admin | Version:
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+------------------------------------

Comment (by Collin Anderson):

It can be done with inlines, but not with easy left/right select widget.

--
Ticket URL: <https://code.djangoproject.com/ticket/897#comment:45>

Django

unread,
Jul 15, 2023, 5:39:35 PM7/15/23
to django-...@googlegroups.com
#897: Bi-Directional ManyToMany in Admin
-------------------------------+------------------------------------
Reporter: anonymous | Owner: nobody
Type: New feature | Status: new
Component: contrib.admin | Version:
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+------------------------------------
Description changed by khaled5321:

Old description:

> Allow amnytomany relationships to be defined both ways
> E.G
>
> {{{
> 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.
>
> Thanks

New description:

Allow manytomany relationships to be defined both ways
E.G

{{{
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 screen I get a
multiselect window for item types.

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

Thanks

--

--
Ticket URL: <https://code.djangoproject.com/ticket/897#comment:47>

Reply all
Reply to author
Forward
0 new messages