0

アカウントの情報の一部を表示するために、次のコードを表示しています。ORMを介してこれを機能させるために何時間も試しましたが、機能させることができませんでした。最終的には生のSQLで実行しましたが、必要なのはそれほど複雑ではありません。ORMでできると確信しています。

最後に、いくつかのテーブルからディクショナリaccountDetailsにデータを入力したいと思います。

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))
4

1 に答える 1

2

モデルを投稿すると、より簡単になります。しかし、SQL から、モデルは次のようになっていると想定しています。

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'

これらのモデルに基づく:

accountDetails = XenPanelSubscription.objects.filter(customer_id = request.user.id)
for accountDetail in accountDetails:
    print accountDetail.hostname, accountDetail.distro, accountDetail.node.location
于 2009-06-30T19:01:11.073 に答える