从3个表中获取彼此相关的数据并将其显示在模板中 - django

时间:2017-09-05 17:23:37

标签: django django-templates django-views django-orm

我想在我的网站上实施一项调查。我为此创建了3个表,下面是每个模型的代码段:

class Survey(models.Model):
    klass_id = models.ForeignKey(Klass, verbose_name='class', related_name='s_klass', on_delete=models.CASCADE)
    question = models.CharField(max_length=100, verbose_name='title')
    description = models.CharField(max_length=500, verbose_name='description', blank=True, null=True)
    end_date = models.DateTimeField(verbose_name='end date')


class SurveyOptions(models.Model):
    survey_id = models.ForeignKey(Survey, verbose_name='survey name', related_name='s_id', on_delete=models.CASCADE)
    text = models.CharField(max_length=50, verbose_name='option title')

class SurveyResponse(models.Model):
    option_id = models.ForeignKey(SurveyOptions, verbose_name='option name', related_name='s_option', on_delete=models.CASCADE)
    person_id = models.ForeignKey(Profile, verbose_name='responser', related_name='responser', on_delete=models.CASCADE)
    create_date = models.DateTimeField(verbose_name='date', auto_now_add=True)

用户对调查做出回应后,我希望获得调查结果并将其显示给模板中的用户。我想展示调查问题,调查选项和调查回复(每个选项及其百分比)。

我在views.py写什么,我在模板中写什么?

第一个模型中的Klass指的是模型。当我转到与每个Klass相关的页面时,我必须查看Klass的所有调查。

Klass指的是大学课程。

1 个答案:

答案 0 :(得分:0)

我喜欢把所有数据都放到模型中,所以这就是我的方法。请注意,我已删除了您的_id,否则您最终会引用Option.survey_id.id以获取ID或Option.survey_id.text,这更令人困惑。

在models.py ::

class Survey(models.Model):
    klass = models.ForeignKey(Klass, verbose_name='class', related_name='s_klass', on_delete=models.CASCADE)
    question = models.CharField(max_length=100, verbose_name='title')
    description = models.CharField(max_length=500, verbose_name='description', blank=True, null=True)
    end_date = models.DateTimeField(verbose_name='end date')

    @property
    def options(self):
        return SurveyOptions.objects.filter(survey=self)



class SurveyOptions(models.Model):
    survey = models.ForeignKey(Survey, verbose_name='survey name', related_name='s_id', on_delete=models.CASCADE)
    text = models.CharField(max_length=50, verbose_name='option title')


    @property
    def response_as_pct(self):
        option_responses = SurveyResponse.objects.filter(option = self).count()
        survey_responses = SurveyResponse.objects.filter(option__in = self.survey.options).count()

        # will need to handle edge cases of 0 options or 0 responses and may not want to multiply by 100
        return option_responses/survey_responses * 100

class SurveyResponse(models.Model):
    option = models.ForeignKey(SurveyOptions, verbose_name='option name', related_name='s_option', on_delete=models.CASCADE)
    person = models.ForeignKey(Profile, verbose_name='responser', related_name='responser', on_delete=models.CASCADE)
    create_date = models.DateTimeField(verbose_name='date', auto_now_add=True)

在Views ::

surveys = Survey.objects.all()   or filter as required

render in whatever way passing surveys

在Template ::

{% for survey in surveys %}
      {% for option in survey.options %}
             {{ option.text }} - {{ option.response_as_pct }}
      {% endfor %}
{% endor %}