views:

115

answers:

2

Hi all,

I have the following code in a view to get some of the information on the account to display. I tried for hours to get this to work via ORM but couldn't make it work. I ended up doing it in raw SQL but what I want isn't very complex. I'm certain it's possible to do with ORM.

In the end, I just want to populate the dictionary accountDetails from a couple of tables.

cursor.execute("SELECT a.hostname, a.distro, b.location FROM xenpanel_subscription a, xenpanel_hardwarenode b WHERE a.node_id = b.id AND customer_id = %s", [request.user.id])
accountDetails = {
    'username': request.user.username,
    'hostname': [],
    'distro': [],
    'location': [],
}

for row in cursor.fetchall():
    accountDetails['hostname'].append(row[0])
    accountDetails['distro'].append(row[1])
    accountDetails['location'].append(row[2])

return render_to_response('account.html', accountDetails, context_instance=RequestContext(request))
+2  A: 

It would be easier if you post models. But from SQL I'm assuming the models are like this:

class XenPanelSubscription(models.Model):
    hostname = models.CharField()
    distro = models.CharField()
    node = models.ForeignKey(XenPanelHardwareNode)
    customer_id = models.IntegerField()

    class Meta:
        db_table = u'xenpanel_subscription'

class XenPanelHardwareNode(models.Model):
    location = models.CharField()

    class Meta:
        db_table = u'xenpanel_hardwarenode'

Based on these models:

accountDetails = XenPanelSubscription.objects.filter(customer_id = request.user.id)
for accountDetail in accountDetails:
    print accountDetail.hostname, accountDetail.distro, accountDetail.node.location
zdmytriv
A: 

Awesome, thanks. I tried for 2 hours yesterday and couldn't get the syntax just right. Thanks again.

--seth

Seth