Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iterating over related objects in django

Tags:

python

django

I have these models:

class Person(models.Model):
   name=models.CharField(max_length=100)
   family=models.CharField(max_length=100)

class MailContact(models.Model):
   person=models.ForeignKey(Person)
   email=models.CharField(max_length=100)
   #some fields

class Participant(models.Model):
   person=models.ForeignKey(Person)
   #some fields

in views.py :

   emails = []
   for participant in participants:
      for contact in participant__person__mailContact_set:
         emails.append(contact.email)
   send_mail(email_subject,email_body,'[email protected]',emails,fail_silently=False)

I've changed this code in many ways but I'm repeatedly get this error:

global name 'participant__person__mailContact_set' is not defined

any suggestion?

like image 518
Asma Gheisari Avatar asked Mar 02 '12 07:03

Asma Gheisari


1 Answers

for contact in participant.person.mailcontact_set.all():
like image 76
DrTyrsa Avatar answered Oct 22 '22 09:10

DrTyrsa