Beginner needs help with models.py for a Survey like Website

49 views
Skip to first unread message

Tobias Dacoir

unread,
Nov 6, 2014, 5:18:02 AM11/6/14
to django...@googlegroups.com
Hi all,

I need to create some kind of survey for my studies but I'm struggling with the models.py. I did read some tutorial on Django and I like it very much, but I'm no expert when it comes to database modellling.

Here is my proposed models.py:

class User(models.Model):

    SEX =   (
        ('m','male'),
        ('f','female')
    )

    user_name = models.CharField(max_length=200)
    user_age = models.IntegerField()
    user_sex = models.CharField(max_length=1, choices=SEX)
    user_count_answers_given = models.IntegerField(default=0)

    def __unicode__(self):
        return self.user_name

# a Collection of Pictures
# a subset of Questions is chosen for each Collection
class Collection(models.Model):
    collection_name = models.CharField(max_length=200)

    def __unicode__(self):
        return self.collection_name


# an Picture is place into exactly one Collection
class Picture(models.Model):
    path = models.CharField(max_length=500)
    title = models.CharField(max_length=200)
   
    def __unicode__(self):
        return self.title


# a List of Questions like:
# - How much do you like this Picture?
# - Is there a girl or a boy in the Picture?
class Question(models.Model):
    question_text = models.CharField(max_length=200)
    collection_enabled = models.ManyToManyField(Collection)

    def __unicode__(self):
        return self.question_text

# Have to save each answer for a specific Picture
# by a specific user and for a specific question
class Answer(models.Model):
    question = models.ForeignKey(Question)
    User = models.ForeignKey(User)
    Picture = models.ForeignKey(Picture)
    choice_value = models.IntegerField()
    timestamp = models.DateField(datetime.now())


Now the problem I have is with the Answer class in general.
I want to create several Collections consisting of different pictures. Also I will have a list of questions. Not all questions should be enabled for each Collection, for example if I create a collection of pictures with no persons in the pictures, it doesn't make sense to ask the user if they see a girl or a boy in the picture.
That is why I added this ManyToMany relationship in Question. I'm not sure if this is the correct way to do this.

Let's assume that I enabled 3 Questions for a certain Collection of Pictures. Now I want to track the answers given by the Users. For some questions the answer should be select by a button labeled 'boy' or 'girl' whereas for likeability it should be selected using a radio button which ranges from 0 (don't like) to 7 (like it very much).
Later on, I need to be able to see for each picture how many people selected which answer (10 people selected boy, 5 people girl and so on). Also I want to calculate an average score for likeability. I want to display this score directly after the user gave his input.
So I thought that I need additional fields in the Picture class to safe the average score, but what if my question list changes? Then I need to update the Picture class every time and add an additional field for each new question. That doesn't make sense. I would need something like a Decorator pattern - but I have no idea if this is possible in databases, or how to model it.

Can anyone please help me with this?

Collin Anderson

unread,
Nov 12, 2014, 3:28:59 PM11/12/14
to django...@googlegroups.com
Hello,

It seems to me you should merge your Picture and Collection model, right? because there's a collection of questions for each picture?

So I thought that I need additional fields in the Picture class to safe the average score, but what if my question list changes? Then I need to update the Picture class every time and add an additional field for each new question. That doesn't make sense. I would need something like a Decorator pattern - but I have no idea if this is possible in databases, or how to model it.

It might make sense to save it on a "through" table in your many to many.

Collin

Reply all
Reply to author
Forward
0 new messages