Tuesday, June 29, 2010

Re: How can I access attributes in intermediate class

I was intrigued so I did it, I get this solution:

from models import User, Group, Membership

def example(request):
    example_group_id = 1 #I create only one group to test, this is his id

    group = Group.objects.get(pk=example_group_id)
    for user in group.members.iterator(): #Get all user in the group throught the membership
        membership = Membership.objects.get(person=user, group=group) #Get the membership object using the user and the group as keys
        membership.date_joined # Now you can read your date_joined

Perhaps it isn't the best way to do it, but it works :p

I hope this helps you,
Álex González

On Tue, Jun 29, 2010 at 19:45, cat in a tub <lujooo@gmail.com> wrote:
class User(models.Model):
   name = models.CharField(max_length=128)

class Group(models.Model):
   name = models.CharField(max_length=128)
   members = models.ManyToManyField(User, through='Membership')

class Membership(models.Model):
   person = models.ForeignKey(User)
   group = models.ForeignKey(Group)
   date_joined = models.DateField()


What if I want to access Membership.data_joined in Group class?

Thanks

--
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+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.




--
Please, don't send me files with extensions: .doc, .docx, .xls, .xlsx, .ppt and/or .pptx
http://mirblu.com

--
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+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment