【问题标题】:What Query Sets can help me produce results dependent on two Choice Fields?哪些查询集可以帮助我根据两个选择字段生成结果?
【发布时间】:2019-06-30 02:27:00
【问题描述】:

我的项目在其数据库中存储了许多物理问题,其中每个问题都属于物理主题和问题类型。 我有两个ChoiceField:
* 一个主题,包括 16 个主题。
* 一种问题类型,包括两种问题类型。

我有一个提交按钮,应该显示我的过滤结果,但是,我不知道如何在 views.py 中编写查询集,尽管我已经阅读了文档但仍然不知道如何进行一个或多个查询以获得我的结果。

这是models.py

            from django.db import models
            from home.choices import *

            # Create your models here.

            class Topic(models.Model):
                topic_name = models.IntegerField(
                                choices = question_topic_name_choices, default = 1)
                def __str__(self):
                    return '%s' % self.topic_name

            class Image (models.Model):
                image_file = models.ImageField()

                def __str__(self):
                    return '%s' % self.image_file

            class Question(models.Model):
                question_type = models. IntegerField(
                                choices = questions_type_choices, default = 1)
                question_topic = models.ForeignKey(    'Topic',
                                                on_delete=models.CASCADE,
                                                blank=True,
                                                null=True)
                question_description = models.TextField()
                question_answer = models.ForeignKey(    'Answer',
                                                on_delete=models.CASCADE,
                                                blank=True,
                                                null=True)
                question_image = models.ForeignKey(    'Image',
                                                on_delete=models.CASCADE,
                                                blank=True,
                                                null=True)

                def __str__(self):
                    return '%s' % self.question_type

            class Answer(models.Model):
                answer_description = models.TextField()
                answer_image = models.ForeignKey(    'Image',
                                                on_delete=models.CASCADE,
                                                blank=True,
                                                null=True)

                def __str__(self):
                    return '%s' % self.answer_description

这是forms.py

            from django import forms
            from betterforms.multiform import MultiModelForm
            from .models import Topic, Image, Question, Answer
            from .choices import questions_type_choices, question_topic_name_choices

            class TopicForm(forms.ModelForm):
                topic_name      =   forms.ChoiceField(
                                choices=question_topic_name_choices,
                                widget = forms.Select(
                                attrs = {'class': 'home-select-one'}
                                    ))

                class Meta:
                    model = Topic
                    fields = ['topic_name',]
                    def __str__(self):
                        return self.fields


            class QuestionForm(forms.ModelForm):
                question_type =   forms.ChoiceField(
                                choices= questions_type_choices,
                                widget = forms.Select(
                                attrs = {'class': 'home-select-two'},
                                    ))

                class Meta:
                    model = Question
                    fields = ['question_type',]
                    def __str__(self):
                        return self.fields


            class QuizMultiForm(MultiModelForm):
                form_classes    =   {
                            'topics':TopicForm,
                            'questions':QuestionForm
                }

这是views.py

        from django.shortcuts import render, render_to_response
        from django.views.generic import CreateView, TemplateView
        from home.models import Topic, Image, Question, Answer
        from home.forms import QuizMultiForm

        def QuizView(request):
            if request.method == "POST":
                form = QuizMultiForm(request.POST)
                if form.is_valid():
                    pass
            else:
                form = QuizMultiForm()
            return render(request, "index.html", {'form': form})

这是index.html

{% extends 'base.html' %} {% block content %}
<form method="POST">
  {% csrf_token %} {{ form.as_p }}
  <button type="submit" id="home-Physics-time-button">
    It is Physics Time</button>
</form>
{% endblock content %}

任何帮助都会很棒。谢谢!

【问题讨论】:

    标签: django python-3.x django-forms django-views django-queryset


    【解决方案1】:

    我不知道您到底要过滤什么,或者我是否理解正确(还不能添加 cmets),但这里是一个示例:

    views.py

    def QuizView(request):
                   topics = Topic.objects.filter(topic_name=1) # i dont know your choices, but i go with the set default
                    if request.method == "POST":
                        form = QuizMultiForm(request.POST)
                        if form.is_valid():
                            pass
                    else:
                        form = QuizMultiForm()
                    return render(request, "index.html", {'form': form, 'topics':'topics})
    

    现在调用查询的模板部分

    {% for topic in topics %}
    <h1> {{ topic.topic_name }} </h1>
    {% endfor %}
    

    说明:您正在通过 .filter(model_field=) 过滤视图中的查询 在您的模板中,您遍历所有结果(您通过花括号中的上下文参数将视图中的“主题”传递到模板中),并由视图过滤

    【讨论】:

    • 我的最终目标是,当我单击提交按钮时,将转到另一个模板,其上下文是根据用户输入的问题存储在数据库中的问题。我知道要做到这一点,我必须创建一个新视图并用它渲染一个模板。我不知道该怎么做,我会争取任何我能得到的帮助。谢谢!
    • 如何从 Django 选择字段中检索选定的数据?我有两个(主题和问题类型)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-10-23
    • 2016-09-22
    • 2013-06-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多