On Thu, 2009-01-15 at 10:35 -0800, Evgeniy Ivanov (powerfox) wrote:
> Hi list,
> I want to have ManyToMany fields in both models to make django
> generate forms with multiselect for both models.
> Something like this:
> 
> class User(models.Model):
>    groups = models.ManyToManyField('Group', related_name='groups')
> class Group(models.Model):
>    users = models.ManyToManyField(User, related_name='users')
> 
> First of all without related_name specified this code doesn't work.
> Secondly it will create 2 link/relation tables which can surprise new
> django users.

It's completely logical. You've specified two separate relations here,
not the same relation from both ends.

[...]
> How can I add m2m to both models without using third model with
> foreign keys (or with it, but without extra ID field)? If to be
> sincere it doesn't make much sense, but everything in Django should be
> perfect :)

You only specify the field on one model and it's available at both ends.
This is a design feature so that you *don't* have to write the same
thing twice. So the answer to your question is to specify the ManyToMany
field on one model.

So the solution to your problem is "don't do that". "Perfect" doesn't
mean you can use whatever syntax you like. In Django, each time you
write ManyToManyField, you are specify a new relation between two
models.

You need to go back and look at your original problem. You wanted forms
with multiselect fields for both forms. So the real question is "how do
you do that". I suspect the answer is that you have to manually
construct one of the forms (which you can write a helper function to do
if you need this in a lot of projects, or, if this only happens for one
model, just write a single form class). The other answer is to rethink
your data entry so that this kind of bi-directional multi-select isn't
necessary. It's not a really common UI pattern and you can probably
think of ways to avoid it.

Regards,
Malcolm


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to