How to apply condition in django admin form? -
i have models in 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)
and have admin.py this:
from django.contrib import admin quiz.models import navigation, category, question, choice, answer, questiontype class choiceinline(admin.tabularinline): model = choice = 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 2 headings while displaying questions list_display = ('question', 'publication_date') # add filter option list_filter = ['publication_date'] # add search capability search_fields = ['question'] # show latest elements default date_hierarchy = 'publication_date' admin.site.register(question, questionadmin) admin.site.register(navigation)
there 2 types of questions: multiple choice questions, text based questions.
while creating question, want show choices if user select multiple choice question , show text based 1 input if user select text based question. how can that?
so while creating question, want show choices if user select multiple choice question , show text based 1 input if user select text based question. how can that?
you need javascript since that's happening during creation.
follow these steps:
- in questionadmin change create template
- in new template add javascript hide both fields
- add javascript show appropriate field depending on selection
if doesn't have javascript working, see both fields.
show code if struggle 1 of these steps.
Comments
Post a Comment