Unsure what the easiest method to describe the issue is. I've 2 tables contact and attribute. The contact table has 1 entry per person and also the attribute table has , 1, or many records per person. They're became a member of presently having a "fake" foreign key that is not a real foreign key. If I have to add the foreign key I'll it's not a large deal just coping with old data and there is no foreign key initially. Therefore the tables are organized the following:

contact:

class contact(models.Model):
    contactId = models.AutoField(primary_key=True, db_column='contactId')
    firstName = models.CharField(max_length=255, null=True, db_column='firstName')
    middleName = models.CharField(max_length=255, null=True, db_column='middleName')
    lastName = models.CharField(max_length=255, null=True, db_column='lastName')

attribute:

class attribute(models.Model):
    attributeId = models.AutoField(primary_key=True, db_column='attributeId')
    contactId = models.IntegerField(db_index=True, null=True, db_column='contactId')
    attributeValue = models.TextField(null=True, db_column='attributeValue')

And So I have properly setup the Django models to represent these tables. Ok now what I have to accomplish is really a view and template to loop of these tables so that it creates an xml doc within the following format:

<contacts>
    <contact>
        <contactId></contactId>
        <firstName></firstName>
        <lastName></lastName>
        <attributes>
            <attribute>
                <attributeId></attributeId>
                <attributeValue></attributeValue>
            </attribute>
        </attributes>
    </contact>
</contacts>

So there will be a report on all contacts and every one of the characteristics connected with every contact.

I am certain there's an easy way to do this. In other languages I'd simple write two looping queries to loop within the contact after which loop within the characteristics for every contact. However the organization Sometimes for is moving to a different platform and wish the brand new application designed in django/python each of which I'm still learning.

Any help that anybody can offer is appreciated.

Presuming you've setup your django models to apply your current database setup, I'd perform the following basically did not possess a foreign key setup.

contacts = Contact.objects.all()

for contact in contacts:
    contact.attributes = Attribute.objects.filter(contactId=contact.pk) 


return render_to_response("mytemplate.html", {'contacts': contacts })

Alternately, avoid wasting queries

attributes_map = dict( 
    [(attribute.contactId, attribute) for attribute in \
        Attribute.objects.filter(contactId__in=[contact.pk for contact in contacts])]
    )

for contact in contacts:
    contact.attributes = attributes_map.get(contact.pk)

return render_to_response("mytemplate.html", {'contacts': contacts })

Template

<contacts>
{% for contact in contacts %}
    <contact>
        <contactId>{{ contact.pk }}</contactId>
        <firstName>{{ contact.firstName }}</firstName>
        <lastName>{{ contact.lastName }}</lastName>

        {% if contact.attributes %}
            <attributes>
            {% for attribute in contact.attributes %}
                <attribute>
                    <attributeId>{{ attribute.pk }}</attributeId>
                    <attributeValue>{{ attribute.attributeValue }}</attributeValue>
                </attribute>
            {% endfor %}
            </attributes>
        {% endif %}

    </contact>
{% endfor %}
</contacts>