Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 233065
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T20:04:11+00:00 2026-05-11T20:04:11+00:00

I am writing an app for a simple survey. For possible answers I need

  • 0

I am writing an app for a simple survey.

For possible answers I need “Yes/Now”, “1 out of 1 to 5”, and a short text

In the admin it should be selectable, what kind of answer should be given.

My models:

from django.db import models
from django.contrib.contenttypes.models import ContentType

CHOICES=((1,'excactly true'),(2,'mostly true'),(3,'mostly untrue'),(4,'untrue'),(5,'I don\'t know '))
class Answer(models.Model):
    question = models.ForeignKey("Question")

class ChoiceAnswer(Answer):
    answer = models.IntegerField(max_length=1, choices=CHOICES)
    def __unicode__(self):
        return u'%s: %s'%(self.question, self.answer)

class TextAnswer(Answer):
    answer= models.CharField(max_length=255)
    def __unicode__(self):
        return u'%s: %s'%(self.question, self.answer)

class BooleanAnswer(Answer):
    answer= models.BooleanField(choices=((True,'yes'),(False,'no')))
    def __unicode__(self):
        return u'%s: %s'%(self.question, self.answer)

class Question(models.Model):
    question = models.CharField(max_length=255)
    answer_type = models.ForeignKey(ContentType)

    def __unicode__(self):
        return u'%s'%self.question

Is there a (hopefully: simple) way for generating the form by looping over all questions and creating the an answer form compatible to the question’s answer_type?

And is it possible to filter contenttypes for answer_type = models.ForeignKey(ContentType) so that only the answertypes will be shown?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T20:04:11+00:00Added an answer on May 11, 2026 at 8:04 pm

    As I discovered a solution myself I will answer my own question:

    models.py:

    CHOICES=((1,'exactly true'),(2,'mostly true'),(3,'mostly untrue'),(4,'untrue'),(5,'I don\'t know '))
    
    class Answer(models.Model):
        question = models.ForeignKey("Question")
    
    class ChoiceAnswer(Answer):
        answer = models.IntegerField(max_length=1, choices=CHOICES)
        def __unicode__(self):
            return u'%s: %s'%(self.question, self.answer)
    
    class TextAnswer(Answer):
        answer= models.TextField()
        def __unicode__(self):
            return u'%s: %s'%(self.question, self.answer)
    
    class BooleanAnswer(Answer):
        answer= models.BooleanField(choices=((True,'yes'),(False,'no')))
        def __unicode__(self):
            return u'%s: %s'%(self.question, self.answer)
    
    class Question(models.Model):
        question = models.CharField(max_length=255)
        answer_type = models.ForeignKey(ContentType)
    
        def __unicode__(self):
            return u'%s'%self.question
    

    forms.py:

    class ChoiceAnswerForm(forms.ModelForm):
        class Meta:
            model = ChoiceAnswer
            exclude=("question",)
    ChoiceAnswer.form = ChoiceAnswerForm
    
    class BooleanAnswerForm(forms.ModelForm):
        class Meta:
            model = BooleanAnswer
            exclude=("question",)
    BooleanAnswer.form= BooleanAnswerForm
    
    class TextAnswerForm(forms.ModelForm):
        class Meta:
            model = TextAnswer
            exclude=("question",)
    TextAnswer.form = TextAnswerForm
    

    the view:

    #needed for monkey-patching.
    from survey.forms import BooleanAnswerForm, TextAnswerForm, ChoiceAnswerForm 
    
    def index(request):
        questions = Question.objects.all() 
        if request.method == 'POST': # If the form has been submitted...
            print request.POST
            for q in questions :
                try:
                    data ={ u'%s-answer'%q.id: request.POST[u'%s-answer'%q.id]}
                except:
                    data = { u'%s-answer'%q.id: None}
                q.form = q.answer_type.model_class().form(prefix="%s"%q.id, data=data)    
        else:
            for q in questions :
                q.form = q.answer_type.model_class().form(prefix="%s"%q.id) 
    
        return render_to_response('survey.html', {
            'questions': questions,
    
        })  
    

    and in the template:

    {% block content %}
    
        <div class="survey">
            <form enctype="multipart/*" action="/" method="post">
    
            {% for question in questions %}
                <p>{{ question.question }}</p><ul>{{ question.form.as_ul }}</ul>
            {% endfor %}
    
        <div><input type="submit" value="submit" /></div>
    </form>
    </div>
    
    {% endblock %}
    

    The monkey-patching could be avoid by some sort of registering. But for now I am fine with this.

    EDIT

    the contentype-filtering can be done like

    answer_type = models.ForeignKey(ContentType, 
                  limit_choices_to = Q(name='text answer', app_label='survey')| \
                                     Q(name='boolean answer', app_label='survey')| \
                                     Q(name='choice answer', app_label='survey'))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a simple app. I need to block user from a page if
I am writing a really simple app which should be quick to use. I
I am writing a simple app used for communicating between a server and an
I'm writing a simple app with AppEngine, using Python. After a successful insert by
I'm currently writing a simple app that performs a series of requests to the
I'm new to android and am writing a simple app that calculates the distance
I am currently writing a simple IOS app that saves tasks into a table.
I'm writing a simple web app in PHP that needs to have write access
I'm writing a simple iPhone app which lets a user access a series of
Writing a test app to emulate PIO lines, I have a very simple Python/Tk

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.