Hi all,

I don't have a technical background, so please be gentle with me.

I have two models with a ForeignKey relation: "couple" and "woman". I am 
trying to set up a query that lists the women with lastname='Test' along with 
some of their information from the "woman" model as well as some from "couple" 
model. I am trying to use the examples in the tutorial / documentation 
sections but I couldn't get two different datasets from two different models 
displayed together. Briefly, I would like to display lastname, firstname, date 
of birth and email fields from the "woman" model, and city field from the 
"couple" model for these ladies with a last name of Test (n=2). Any tips on 
how to accomplish this will be very much appreciated. Please see related info 
below, which BTW does not work:

----------------------
models.py:

class Couple(models.Model):
    couple_name = models.CharField(max_length=100)
    entry_date = models.DateField('Date Entered')
    home_address1 = models.CharField(max_length=200)
    home_address2 = models.CharField(max_length=200)
    city = models.CharField(max_length=100)
    state = models.CharField(max_length=2, null=True)
    country = models.CharField(max_length=100, default="United States")
    home_phone = models.CharField(max_length=20)
    def __unicode__(self):
        return self.couple_name

class Woman(models.Model):
    couple = models.ForeignKey(Couple, unique=True)
    lastname = models.CharField(max_length=100)
    firstname = models.CharField(max_length=100)
    middle_initial = models.CharField(max_length=1, null=True)
    ssn = models.PositiveIntegerField('SSN')
    dob = models.DateField('Date of Birth')
    cell_phone = models.CharField(max_length=20, null=True)
    email = models.EmailField(null=True)


view.py:
...
def index(request):
   couple_query = Woman.objects.filter(lastname__exact='Test')
   cn = Couple.objects.get(id__exact=woman.id)
   return render_to_response('kimlik/index.html', {'couple_query': 
couple_query, 'cn': cn})


index.html:
...
{% if couple_query %}
    <ul>
    {% for woman in couple_query %}
      cn = Couple.objects.get(id__exact=woman.id)
      <li>{{ woman.lastname }}, {{ woman.firstname }}, {{ woman.dob }}, {{ 
woman.email }}, {{ cn.city }}</li>
    {% endfor %}
    </ul>
{% else %}
    <p>No couple is available.</p>
{% endif %}

Attachment: signature.asc
Description: This is a digitally signed message part.

Reply via email to