2

I'm working on a questionnaire model for django that should be maintainable by someone with no programming experience, so I've spent a lot of time crafting my models to compensate for minor details.. Now I want to unlock the potential of SQL database queries to be able to generate statistics about the responses and feedback given.

One of my question types is a 5 star rating, so I would like to be able to gather statistics about the question like:

  • How many responses for question q were 5 star (, 4star, 3star, etc.)?
  • What was the average rating response?

Ideally I would like to record these statistic questions in a model, and create a view that shows all the statistics asked and keep the entire thing programmatic.

Should this be a carefully crafted model or set of models like feedback, or is there already some framework or module that handles these situations for me?

My questionnaire/models.py:

class QuestionType(models.Model):
    name = models.CharField(max_length=256, blank=True, default="")

class Question(models.Model):
    text = models.TextField()
    type = models.ForeignKey(QuestionType)

class Response(models.Model):
    question = models.ForeignKey(Question)
    answer = models.TextField()

class Feedback(models.Model):
    user = models.ForeignKey(User)
    responses = models.ManyToManyField(Response)
    response_time = models.DateTimeField(auto_now_add=True)
steve-gregory
  • 7,396
  • 8
  • 39
  • 47
  • Is there a specific reason you want to split up `Feedback` and `Reponse` like that? Can one answer be given to many questions? – Jesse the Game Nov 15 '12 at 15:17
  • I split up feedback and response because there are other pieces of the model that have been pulled out to keep "Just the important stuff" in this question. A Response doubles as a set of answers for a 'select' question or rating question.. As an example, a question "How would you rate ..." would have 5 possible responses (and ONLY 5) 1 star, 2 star, etc. As well, 'Single Select' and "Multi Select" questions would also have a specific set of responses. This question was not about how to modify these models but rather about how to model the *statistics* I would like to gather from these models. – steve-gregory Nov 15 '12 at 20:25

1 Answers1

0

This would cover your requirements:

class QuestionType(models.Model):
    name = models.CharField(max_length=256, blank=True, default="")

class Question(models.Model):
    text = models.TextField()
    type = models.ForeignKey(QuestionType)

    def how_many_ratings_where_x_stars(self, stars):
        return self.rating_set.filter(stars=stars).count()

    def average_rating(self, stars):
        return self.rating_set.aggregate(models.Avg('stars'))['stars__avg']

class Response(models.Model):
    question = models.ForeignKey(Question)
    answer = models.TextField()
    user = models.ForeignKey(User)
    timestamp = models.DateTimeField(auto_now_add=True)

class Rating(models.Model):
    question = models.ForeignKey(Question)
    stars = models.PositiveIntegerField(min_value=1, max_value=5)
    user = models.ForeignKey(User)
    timestamp = models.DateTimeField(auto_now_add=True)

    class Meta:
        unique_together = [('question', 'user')]
Jesse the Game
  • 2,600
  • 16
  • 21
  • As i said earlier, the model was only meant to show what i'm dealing with but does not need to be changed, I would like to know what (Model or some other way) I should do to gather statistics about the model.. Such as: How many people answered 'Yes' or 'No' to (question ...), What was the average rating given to (...), etc. – steve-gregory Nov 15 '12 at 20:31