ManyToManyField in both models/forms

29 views
Skip to first unread message

Evgeniy Ivanov (powerfox)

unread,
Jan 21, 2009, 4:38:03 PM1/21/09
to Django developers
Hi list,
I had to implement M2M editing in both admin forms (something like
editing userlist for group and grouplist for user). I wasn't able to
find any fine solution, but specifying M2M in both models and creating
tables manually (since syncdb tries to create two tables).
I herd in the IRC, that such thing (editing in both forms) is
abnormal, but I disagree.
So I tried to use intermidiary model like described in the docs, but
it is really another thing (not "pure" m2m). Another solution is
hardcording the form, but it will require extra save code, I don't
like it too.

I think that if you add special arg (create_table) to ManyToManyField
it can be easily implemented using models/fields/related.py:751.

If you think it can go, I will create a ticket.

Also there is (a kind of offtop) a very cute thing:
http://www.djangosnippets.org/snippets/962/
Why isn't it in the native ManyToManyField?

Evgeniy Ivanov (powerfox)

unread,
Jan 22, 2009, 8:38:03 AM1/22/09
to Django developers
Reread what I've written and understand it would be much better with a
sample:

class User(models.Model):
groups = models.ManyToManyField('Group', related_name='groups',
db_table=u'USERS_TO_GROUPS')
class Group(models.Model):
users = models.ManyToManyField(User, related_name='users',
db_table=u'USERS_TO_GROUPS')

This code works fine with existing DB and also it really helps to
generate proper form from the model. But syncdb fails since it tries
to create 2 tables with the same name.

class User(models.Model):
groups = models.ManyToManyField('Group', related_name='groups',
db_table=u'USERS_TO_GROUPS')
class Group(models.Model):
users = models.ManyToManyField(User, related_name='users',
db_table=u'USERS_TO_GROUPS', create_table=False)

Works fine.

Also there was some discussion in django users:
http://groups.google.com/group/django-users/browse_thread/thread/50bf564e98954a78

On Jan 22, 12:38 am, "Evgeniy Ivanov (powerfox)"

Yuri Baburov

unread,
Jan 22, 2009, 10:51:53 AM1/22/09
to django-d...@googlegroups.com
Hi, and what's wrong with writing a fix for admin to make "inverted"
m2m relation to show m2m if specified in list_display list? I know
it's not easy to do for one who is not django creator, but since it's
open source... ;)

Core devs, what's your opinion?

Such change is pretty logical, short and non-intrusive.
--
Best regards, Yuri V. Baburov, ICQ# 99934676, Skype: yuri.baburov,
MSN: bu...@live.com

Evgeniy Ivanov

unread,
Jan 22, 2009, 4:58:54 PM1/22/09
to Django developers
On Jan 22, 6:51 pm, Yuri Baburov <burc...@gmail.com> wrote:
> Hi, and what's wrong with writing a fix for admin to make "inverted"
> m2m relation to show m2m if specified in list_display list? I know
> it's not easy to do for one who is not django creator, but since it's
> open source... ;)

Oh, it could be interesting, but no time even for my native KDE
org :/

>
> Core devs, what's your opinion?
>
> Such change is pretty logical, short and non-intrusive.
>

Thanks Yuri, you're the first person who is not against such change.
Maybe they're right and who wants can write a subclass like:
http://www.djangosnippets.org/snippets/1295/
But I feel that with a keyword argument it can be similar to
"through".

Since it's all about keywords (a kind of) I want to point to
http://www.djangosnippets.org/snippets/962/ again.
It's not mine, but there should be no arguing about such change.

Yuri Baburov

unread,
Jan 22, 2009, 7:09:22 PM1/22/09
to django-d...@googlegroups.com
On Fri, Jan 23, 2009 at 3:58 AM, Evgeniy Ivanov <lolkaa...@gmail.com> wrote:
>
> On Jan 22, 6:51 pm, Yuri Baburov <burc...@gmail.com> wrote:
>> Hi, and what's wrong with writing a fix for admin to make "inverted"
>> m2m relation to show m2m if specified in list_display list? I know
>> it's not easy to do for one who is not django creator, but since it's
>> open source... ;)
>
> Oh, it could be interesting, but no time even for my native KDE
> org :/
>
>>
>> Core devs, what's your opinion?
>>
>> Such change is pretty logical, short and non-intrusive.
>>
>
> Thanks Yuri, you're the first person who is not against such change.
I'd change/add some part of admin code if i had time.
Single type of search, disability to have 2 views for same model,
widgets & filters not working for large number of items, lack of
read-only items, bad extensibility, some missing hooks and naive ajax
non-compatibility frustrate me anyway. I've written some cool
extensions, but they are private now.

Malcolm Tredinnick

unread,
Jan 22, 2009, 7:17:40 PM1/22/09
to django-d...@googlegroups.com
On Thu, 2009-01-22 at 21:51 +0600, Yuri Baburov wrote:
> Hi, and what's wrong with writing a fix for admin to make "inverted"
> m2m relation to show m2m if specified in list_display list? I know
> it's not easy to do for one who is not django creator, but since it's
> open source... ;)
>
> Core devs, what's your opinion?

You're proposing something a bit different to the topic of the thread.
The original poster is arguing for a change to the way data is described
in order, solely, to affect the presentation. That's a fairly egregious
hack and I'm strongly against that idea for precisely that reason --
it's symptom patching, not problem solving. I suggested in the
django-users thread that was referenced that approaching this as a form
field problem (which is what it is) is the right approach.

Addressing this only in the admin would be short-sighted, however. A for
field that new how to handle reverse relations would be the first step
and then allowing the admin to use that is the second one.

>
> Such change is pretty logical, short and non-intrusive.

I look forward to reading your patch. :-)

Regards,
Malcolm

Yuri Baburov

unread,
Jan 22, 2009, 9:45:11 PM1/22/09
to django-d...@googlegroups.com
Hi Malcolm,

> I look forward to reading your patch. :-)

OK.

Evgeniy Ivanov

unread,
Jan 23, 2009, 3:12:21 AM1/23/09
to Django developers
On Jan 23, 3:17 am, Malcolm Tredinnick <malc...@pointy-stick.com>
wrote:
> Addressing this only in the admin would be short-sighted, however. A for
> field that new how to handle reverse relations would be the first step
> and then allowing the admin to use that is the second one.
>

Maybe better approuch is in model's meta? It will give forms a chance
to do this fancy thing. And it is a kind of logical, since model
really has such relation.

Malcolm Tredinnick

unread,
Jan 23, 2009, 10:53:49 PM1/23/09
to django-d...@googlegroups.com

No. Because Meta is not about presentation. It's part of the model,
which is defining how the data is stored and retrieved. This whole issue
is *purely* presentational. Attempting to change the model class to
"solve" it is simply using the wrong shovel to hammer in your screws.

Regards,
Malcolm


Evgeniy Ivanov

unread,
Jan 24, 2009, 3:31:25 AM1/24/09
to Django developers
Oops, sorry. I meant form's meta.
Something like this:
class GroupForm(ModelForm):
class Meta:
model = Group
m2m_model = User

Looks like more logical thing, than I suggested at the beginning.

On Jan 24, 6:53 am, Malcolm Tredinnick <malc...@pointy-stick.com>
Reply all
Reply to author
Forward
0 new messages