Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Django:如何显示选项的名称而不是整数?_Python_Django_Django Models_Django Forms_Django Views - Fatal编程技术网

Python Django:如何显示选项的名称而不是整数?

Python Django:如何显示选项的名称而不是整数?,python,django,django-models,django-forms,django-views,Python,Django,Django Models,Django Forms,Django Views,我有几个问题,而使这个网站,这是最新的一个。我已经创建了这些选项字段,但是,只有数字显示在第一个字段中,而在第二个字段中,由于问题类型链接到问题表,我添加到数据库中的问题将显示,而不是问题类型,只有两种类型的问题应该出现。根据用户的选择,只用于阅读的新页面将显示属于此特定主题的问题以及特定类型的问题 这是models.py from django.db import models from home.choices import * # Create your mode

我有几个问题,而使这个网站,这是最新的一个。我已经创建了这些选项字段,但是,只有数字显示在第一个字段中,而在第二个字段中,由于
问题类型
链接到
问题
表,我添加到数据库中的问题将显示,而不是问题类型,只有两种类型的问题应该出现。根据用户的选择,只用于阅读的新页面将显示属于此特定主题的问题以及特定类型的问题

这是
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):
        questions_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
    question_topic_name_choices = (
        (1, ("Topic #1: Measurements and Uncertainties")),
        (2, ("Topic #2: Mechanics")),
        (3, ("Topic #3: Thermal Physics")),
        (4, ("Topic #4: Waves")),
        (5, ("Topic #5: Electricity and Magnetism")),
        (6, ("Topic #6: Circular Motion and Gravitation")),
        (7, ("Topic #7: Atomic, Nuclear and Particle Physics")),
        (8, ("Topic #8: Energy Production")),
        (9, ("Topic #9: Wave Phenomena (HL Only)")),
        (10, ("Topic #10: Fields (HL Only)")),
        (11, ("Topic #11: Electromagnetic Induction (HL Only)")),
        (12, ("Topic #12: Quantum and Nuclear Physics (HL Only)")),
        (13, ("Option A: Relativity")),
        (14, ("Option B: Engineering Physics")),
        (15, ("Option C: Imaging")),
        (16, ("Option D: Astrophysics"))
            )

    questions_type_choices = (
        (1, "Multiple Choice Questions"),
        (2, "Problem Solving Questions"))
    from django.shortcuts import render, render_to_response
    from django.views.generic import CreateView
    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})
这是
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.ModelChoiceField(
                        queryset = Topic.objects.all(),
                        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):
        questions_type =   forms.ModelChoiceField(
                        queryset = Question.objects.all(),
                        widget = forms.Select(
                        attrs = {'class': 'home-select-two'},
                        ))

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


    class QuizMultiForm(MultiModelForm):
        form_classes    =   {
                    'topics':TopicForm,
                    'questions':QuestionForm
        }
        def save(self, commit=True):
            objects = super(QuizMultiForm, self).save(commit=False)

            if commit:
                topic_name = objects['topic_name']
                topic_name.save()
                questions_type = objects['question_type']
                questions_type.topic_name = topic_name
                questions_type.save()
            return objects
这是
选项。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):
        questions_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
    question_topic_name_choices = (
        (1, ("Topic #1: Measurements and Uncertainties")),
        (2, ("Topic #2: Mechanics")),
        (3, ("Topic #3: Thermal Physics")),
        (4, ("Topic #4: Waves")),
        (5, ("Topic #5: Electricity and Magnetism")),
        (6, ("Topic #6: Circular Motion and Gravitation")),
        (7, ("Topic #7: Atomic, Nuclear and Particle Physics")),
        (8, ("Topic #8: Energy Production")),
        (9, ("Topic #9: Wave Phenomena (HL Only)")),
        (10, ("Topic #10: Fields (HL Only)")),
        (11, ("Topic #11: Electromagnetic Induction (HL Only)")),
        (12, ("Topic #12: Quantum and Nuclear Physics (HL Only)")),
        (13, ("Option A: Relativity")),
        (14, ("Option B: Engineering Physics")),
        (15, ("Option C: Imaging")),
        (16, ("Option D: Astrophysics"))
            )

    questions_type_choices = (
        (1, "Multiple Choice Questions"),
        (2, "Problem Solving Questions"))
    from django.shortcuts import render, render_to_response
    from django.views.generic import CreateView
    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})
这是
views.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):
        questions_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
    question_topic_name_choices = (
        (1, ("Topic #1: Measurements and Uncertainties")),
        (2, ("Topic #2: Mechanics")),
        (3, ("Topic #3: Thermal Physics")),
        (4, ("Topic #4: Waves")),
        (5, ("Topic #5: Electricity and Magnetism")),
        (6, ("Topic #6: Circular Motion and Gravitation")),
        (7, ("Topic #7: Atomic, Nuclear and Particle Physics")),
        (8, ("Topic #8: Energy Production")),
        (9, ("Topic #9: Wave Phenomena (HL Only)")),
        (10, ("Topic #10: Fields (HL Only)")),
        (11, ("Topic #11: Electromagnetic Induction (HL Only)")),
        (12, ("Topic #12: Quantum and Nuclear Physics (HL Only)")),
        (13, ("Option A: Relativity")),
        (14, ("Option B: Engineering Physics")),
        (15, ("Option C: Imaging")),
        (16, ("Option D: Astrophysics"))
            )

    questions_type_choices = (
        (1, "Multiple Choice Questions"),
        (2, "Problem Solving Questions"))
    from django.shortcuts import render, render_to_response
    from django.views.generic import CreateView
    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})

谢谢大家!

问题是您将queryset作为
formChoiceField
传递,这很好,但将作为DB级别字段值填充。在您的例子中,这些是整数值。一种选择是,您可以将元组选择作为formChoices传递,django将自己处理所有事情。以你的形式

 class TopicForm(forms.ModelForm):
        topic_name      =   forms.ChoiceField(choices=question_topic_name_choices)
        class Meta:
            model = Topic
            fields = ['topic_name',]



    class QuestionForm(forms.ModelForm):
        questions_type =  forms.ChoiceField(choices= questions_type_choices)

        class Meta:
            model = Question
            fields = ['questions_type',]
或者,您可以将DB选项字段从整数字段更改为字符,并存储人类可读的选项名称