Sunday, February 24, 2013

m2m relation just working in one direction

Hi,

I have a problem involving a relation between two entities, Person and
Group.

from django.contrib.auth.models import User
from django.db import models

class Group(models.Model):
employees = models.ManyToManyField('staff.Person',
related_name='groups')
leader = models.OneToOneField('staff.Person',
related_name='led_group')

class Person(User):
pass


$ python manage.py shell

In [1]: from group.models import Group

In [2]: from staff.models import Person

In [3]: p = Person.objects.create(username='person1',
first_name='person 1')

In [5]: g = Group.objects.create(group_name='group 1', leader=p)

In [9]: g.employees.all()
Out[9]: []

In [10]: g.employees.add(p)

In [11]: g.employees.all()
Out[11]: []

In [16]: p.groups.all()
Out[16]: [<Group: group 1>]


As you can see, when I add a person through the group, it's not added to
group.employees but to person.groups. Can you see any mistake in my
code? Thanks in advance for your suggestions :-)

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

No comments:

Post a Comment