* 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.
* 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>
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>
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>
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>
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>
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>
* 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>
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>
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>
Comment (by collinanderson):
#24317 should fix this.
--
Ticket URL: <https://code.djangoproject.com/ticket/897#comment:36>
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>
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>
* status: new => assigned
* owner: nobody => auvipy
* version: => master
--
Ticket URL: <https://code.djangoproject.com/ticket/897#comment:39>
* owner: auvipy =>
* status: assigned => new
--
Ticket URL: <https://code.djangoproject.com/ticket/897#comment:40>
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>
* cc: mmitar@… (added)
--
Ticket URL: <https://code.djangoproject.com/ticket/897#comment:42>
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>
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>
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>
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>