ManyToManyField and Multi Table Inheritance

52 views
Skip to first unread message

Izidor Matušov

unread,
Apr 1, 2014, 7:08:40 AM4/1/14
to django...@googlegroups.com
Hi,

I've run into interesting issue and I'm not sure if I do something wrong or this is Django issue. I'm using Multi Table Inheritance:

<code>
from django.db import models
from django import forms

class AccountType(models.Model):
    users = models.ManyToManyField('auth.User', related_name='accounts')


class AccountTypeA(AccountType):
    name = models.CharField(max_length=255)


class AccountTypeAForm(forms.ModelForm):
    class Meta:
        model = AccountTypeA
        fields = ('name', 'users')
</code>

However, users are not saved in database. Find out that ModelForm calls `self.instance.users = <list-of-users>` but that's not get saved. Even 

<code>
account = AccountTypeA(name='something')
account.save()
account.users = User.objects.all()
</code>

is not propagated. However, account.accounttype.users works perfectly fine. Do I do something wrong?

P.S: I'm using Django 1.6

Izidor

Camilo Torres

unread,
Apr 1, 2014, 2:57:53 PM4/1/14
to django...@googlegroups.com
I tested this with SQLite3 and it worked, using your models:

>>> a.users = User.objects.all()
>>> a.save()
>>> a.delete()
>>> a = AccountTypeA(name='aaaa')
>>> a.save()
>>> a.users = User.objects.all()

After that:
sqlite> select * from testapp_accounttype_users;
1|1|1

Which is what I should expect considering I have only one user and only one account. Could you have an issue in another part of the code?

Regards,
Camilo.
Reply all
Reply to author
Forward
0 new messages