문제

계정의 일부 정보를 표시하려면 다음 코드가 있습니다. 나는 이것을 ORM을 통해 일하기 위해 몇 시간 동안 노력했지만 그것을 작동시킬 수 없었습니다. 나는 원시 SQL로 그것을하게되었지만 내가 원하는 것은 그리 복잡하지 않습니다. ORM과 관련이 있다고 확신합니다.

결국, 나는 단지 몇 개의 테이블에서 사전 계정을 채우고 싶습니다.

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))
도움이 되었습니까?

해결책

모델을 게시하면 더 쉬울 것입니다. 그러나 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
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top