Создайте таблицу отчетов, объединив две модели - PullRequest
0 голосов
/ 01 июля 2019

У меня есть две модели, и я хотел бы объединить эти две модели и создать таблицу, как показано в выходных данных ниже.Пожалуйста, направьте меня с видом и шаблоном кода.

class Question(models.Model):
    question = models.CharField(max_length=200)
    alias = models.CharField(max_length=25)

class Answer(models.Model)
    qst_id = models.ForeignKey(Question,on_delete=models.CASCADE)
    user_phone = models.CharField(max_length=200)
    user_response = models.CharField(max_length=200)
    created_at = models.DateTimeField(auto_now_add=True)

Входные данные: (таблица вопросов)

qid  |  question   |   Alias 
==============================
1       Your name.?    Name 

2       Your Age.?     Age

3       Your Class?    Class 

Данные: таблица ответов

id   | qst_id  |   user_phone  | user_response
================================================
1      1           999999        jhony

2      2           999999        12 

3      3           999999        Second class

4      1           999988        Ramesh

2      2           999988        15 

3      3           999988        First class

Вывод: (ожидается)

S.No    | user_phone  | Name | Age | Class 
============================================
1         999999        jhony   12   second class

2         999988        ramesh  15   First class 

Мой код:

class ReportListlView(LoginRequiredMixin,ListView):
    model = Answers
    template_name  = 'web/test.html'


    def get_context_data(self, **kwargs):
        pk=self.kwargs.get('pk')
        context = super(ReportListlView, self).get_context_data(**kwargs)
        context['answers'] = Answers.objects.all()
        context['question'] = Question.objects.all()

        return context

temaplate / test.html

<div >
<table class='table table-condensed'>
<tr>
     <th>S.No</th>
     <th>Phone Number</th>
    {% for item in question %}
      <th>{{ item.alias  }}</th>
    {% endfor %}
    </tr>
    {% for item in answers %}
    <tr>
    {% ifchanged item.user_phone %}
        <td>{{ forloop.counter }}</td>
        <td>{{ item.user_phone }}</td>
    {% endifchanged %}`enter code here`
          <td>{{ item.user_response }}</td>
    {% ifchanged item.user_phone %}
    </tr>
    {% endifchanged %}`enter code here
    {% endfor %}
  </table>
</div></article>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...