2014-01-02 2 views
1

장고와 파이썬에서 초보자 인 저는 설문 조사 앱을 개발하기 시작했습니다. https://github.com/jessykate/django-survey 몇 가지 기능을 추가했지만 결과 페이지에 문제가 있습니다. 더 정확히 어떻게 데이터를 표시해야합니까? 여기에서 가장 중요한 필드 모델은 어떻게 생겼는지 :Django에서 여러 테이블 쿼리하기

class Survey(models.Model): 
    name = models.CharField(max_length=250) 

class Question(models.Model): 
    text = models.TextField() 
    survey = models.ForeignKey(Survey) 
    choices = models.TextField() 

class Response(models.Model): 
    survey = models.ForeignKey(Survey) 

class AnswerBase(models.Model): 
    question = models.ForeignKey(Question) 
    response = models.ForeignKey(Response) 

class AnswerText(AnswerBase): 
    body = models.TextField(blank=True, null=True) 

class AnswerRadio(AnswerBase): 
    body = models.TextField(blank=True, null=True) 

    and few more Answer.. 

나는이 형식의 데이터는 바 문자로 JS와 디스플레이에서 나중에 처리하는 것이 좋을 것이라고 생각 : 나는 could't

results = [{'some_question_text': 
      [{'answer':'answer1','count': 11},{'answer':'answer2','count': 6}, ..]} 
      ,..] 

가왔다 어떻게 장고 방식으로, 그래서 SQL에서 했어. 문제는 'or ab.id == polls_answerselect.answerbase_ptr_id'쿼리가 이상한 결과를 반환하는 것과 같은 다른 조건을 추가 할 때 하나의 응답 유형에서만 작동합니다. 여기

내가 무슨 짓을했는지의 :

cursor = connection.cursor() 
cursor.execute("select q.text as qtext, ar.body as ans, ab.id as Aid, q.id as Qid, count(ar.body) as count \ 
      from polls_answerbase ab, polls_answerradio ar, polls_question q, polls_survey s \ 
      where ab.id==ar.answerbase_ptr_id \ 
      and ab.question_id==q.id \ 
      and s.id==q.survey_id \ 
      group by ar.body") 
rows = dictfetchall(cursor) 
result = {} 
for r in rows: 
    res[r['qtext']] = [] 
    res[r['qtext']].append({'ans': r['ans'], 'count': r['count']}) 

더 내 문제를 해결하는 올바른 방법은 무엇입니까?

답변

1

설문 조사에 의해 필터링 된 질문 목록이 원하는 것처럼 보이며 json 형식으로 원한다.

을 살펴보십시오. 다중 응답 형식을 지원하는 미리 정의 된 클래스 기반보기 세트가 제공되며, json 중 하나는 그 중 하나입니다. 해당 사이트의 자습서는 설정 과정을 안내하고 간단한 테스트를 통해 올바르게 수행하는지 확인합니다. 모델에 대해 비슷한 작업을 수행 할 수 있습니다.

나는 파이썬/장고 초보자이기도하며 데리러 가기가 쉽다.

관련 문제