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. But according the documentation it's normal behavior. If
I specify db_table in both tables it will still generate 2 tables,
which will make syncdb failed:

#class taken from http://www.djangosnippets.org/snippets/962/
class ManyToManyField(models.ManyToManyField):
    def __init__(self, *args, **kwargs):
        source = kwargs.pop('source_db_column', None)
        reverse = kwargs.pop('reverse_db_column', None)
        if source is not None:
            self._m2m_column_name_cache = source
        if reverse is not None:
            self._m2m_reverse_name_cache = reverse
        super(ManyToManyField, self).__init__(*args, **kwargs)

class User(models.Model):
   groups = ManyToManyField('Group', related_name='groups',
source_db_column='USER_ID',
 
reverse_db_column='GROUP_ID',
 
db_table=u'Users_To_Groups')
class Group(models.Model):
   users = ManyToManyField(User, related_name='users',
source_db_column='GROUP_ID',
 
reverse_db_column='USER_ID',
 
db_table=u'Users_To_Groups')
It will generate 2 identical (only fields order differs) tables
Users_To_Groups. And I guess it will work if I create table manually
(since syncdb doesn't touch already created tables). With my patch
from ticket #10037 it will not have extra id field.

Another variant should work: 
http://docs.djangoproject.com/en/dev/topics/db/models/#intermediary-manytomany
I will get everything working, but with extra ID column in the
database.
Extra ID is created for single ManyToManyField too:
http://code.djangoproject.com/ticket/10037
I think it's wrong.

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 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