0

我在 Django 中有这样的模型:

from django.db import models


class Navigation(models.Model):
    name = models.CharField(max_length=200)

    def __unicode__(self):
        return self.name


class Question(models.Model):
    question = models.TextField()
    publication_date = models.DateField()
    is_active = models.BooleanField(default=1)
    is_premium = models.BooleanField(default=0)

    def __unicode__(self):
        return self.question


class QuestionType(models.Model):
    question = models.OneToOneField(Question)
    question_types = (
                      (1, 'Multiple Choice'),
                      (2, 'Text Answer')
                      )
    type = models.IntegerField(choices=question_types, verbose_name="Select Question Type")


class Category(models.Model):
    question = models.OneToOneField(Question)
    navigation = models.OneToOneField(Navigation)
    choices_options = (
        (1, 'Begineer'),
        (2, 'Intermediate'),
        (3, 'Expert')
    )

    choices = models.IntegerField(choices=choices_options, verbose_name="Select level")


class Choice(models.Model):
    question = models.ForeignKey(Question)
    choice_text = models.CharField(max_length=200)


    def __unicode__(self):
        return self.choice_text

class Answer(models.Model):
    question = models.OneToOneField(Question)
    choice = models.OneToOneField(Choice)

我有这样的 admin.py:

from django.contrib import admin
from quiz.models import Navigation, Category, Question, Choice, Answer, QuestionType



class ChoiceInline(admin.TabularInline):
    model = Choice
    extra = 3


class CategoryInline(admin.TabularInline):
    model = Category


class QuestionTypeInline(admin.TabularInline):
    model = QuestionType

class AnswerInline(admin.TabularInline):
    model = Answer

class QuestionAdmin(admin.ModelAdmin):
    fieldsets = [
    (None,
    {'fields': ['question']}),
    ('Date information', {'fields': ['publication_date'], 'classes': ['collapse']}),
    ]
    inlines = [ChoiceInline, CategoryInline, AnswerInline, QuestionTypeInline]

    # Display two headings while displaying all the questions
    list_display = ('question', 'publication_date')

    # Add filter option
    list_filter = ['publication_date']

    # Add search capability
    search_fields = ['question']

    # Show the latest elements by default
    date_hierarchy = 'publication_date'




admin.site.register(Question, QuestionAdmin)
admin.site.register(Navigation)

将有两种类型的问题:Multiple Choice questions, text based questions.因此,在创建问题时,如果用户选择多项选择问题,我想显示选项,如果用户选择基于文本的问题,则显示基于文本的一个输入。我怎样才能做到这一点?

4

1 回答 1

0

因此,在创建问题时,如果用户选择多项选择问题,我想显示选项,如果用户选择基于文本的问题,则显示基于文本的一个输入。我怎样才能做到这一点?

您需要一些 javascript,因为这是在创建过程中发生的。

按着这些次序:

  1. 在 QuestionAdmin 中更改创建模板
  2. 在这个新模板中添加一些 javascript 来隐藏这两个字段
  3. 添加 javascript 以根据选择显示适当的字段

如果有人没有 javascript 工作,他们会看到这两个字段。

如果您遇到这些步骤之一,请显示您的代码。

于 2013-08-13T06:01:00.297 回答