Re: m2m relation just working in one direction
Mike,
Cool, you are right! Changing the related_name to something different
than groups seems to solve the problem. Thank you very much :-)
Roberto
On 02/24/2013 11:30 PM, Mike Dewhirst wrote:
> Roberto
>
> I'm not entirely sure but it is possible that your group is getting
> mixed up with the contrib.auth group. I would try changing the name of
> my group temporarily to something else just to eliminate that.
>
> Mike
>
> On 25/02/2013 8:58am, Roberto López López wrote:
>> 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.
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home