2009-06-30 7 views
0

표시 할 계정에 대한 정보를 얻기 위해 다음 코드를 볼 수 있습니다. 나는 이것을 ORM을 통해 작동 시키려고 몇 시간 동안 노력했지만 제대로 작동시키지 못했습니다. 나는 원시 SQL에서 그것을 끝내지 만, 내가 원하는 것은 그다지 복잡하지 않다. ORM으로 할 수 있다고 확신합니다.Django 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)) 
+0

포스트 귀하의 모델, 그래서 우리는이없는이 – zinovii

+0

가 함께 감사하지 마십시오 추측 별도의 대답, hehe. 그의 대답을 올바른 것으로 받아들이십시오. – drozzy

답변

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 
관련 문제