views:

62

answers:

1

Hi. Ok, i'm shit at describing. Here's a relationship diag. relationship diag

In Django i've made my models like:

from django.db import models
from datetime import datetime

class Survey(models.Model):
    name = models.CharField(max_length=100)
    pub_date = models.DateTimeField('date published',default=datetime.now)
    def __unicode__(self):
        return self.name

# This model should be abstracted by a more specific model
class Section(models.Model):
    survey = models.ForeignKey(Survey)
    name = models.CharField(max_length=100)
    def __unicode__(self):
        return self.name

# Models for supporting the 'ratings' mode
class RatingSection(Section):
    pass

class RatingQuestion(models.Model):
    section = models.ForeignKey(RatingSection)
    name = models.CharField(max_length=255)
    def __unicode__(self):
        return self.name

class RatingAnswer(models.Model):
    section = models.ForeignKey(RatingSection)
    name = models.CharField(max_length=60)
    def __unicode__(self):
        return self.name

class RatingVotes(models.Model):
    question = models.ForeignKey(RatingQuestion)
    answer = models.ForeignKey(RatingAnswer)
    votes = models.PositiveIntegerField(default=0)
    def __unicode__(self):
        return self.votes + self.answer.name + ' votes for ' + self.question.name

# Models for supporting the 'multichoice' mode
class MultiChoiceSection(Section):
    can_select_multiple = models.BooleanField()

class MultiChoiceQuestion(models.Model):
    section = models.ForeignKey(MultiChoiceSection)
    name = models.CharField(max_length=255)
    def __unicode__(self):
        return self.name

class MultiChoiceAnswer(models.Model):
    section = models.ForeignKey(MultiChoiceSection)
    name = models.CharField(max_length=60)
    votes = models.PositiveIntegerField(default=0)
    def __unicode__(self):
        return self.name

The problem is that I'm almost certain that's not the right way to do it, and even if it is, I can't work out how to allow the admin area in Django to display a selection to the user asking what sub-type of section they want.

What would be the best way to structure models of this sort?

A: 

You could have one section class as well, having an attribute type that could either be rating or multiplechoice - which would be rendered in the admin then as select box.
But I think you should have a look at Django's possibility to create abstract models: http://docs.djangoproject.com/en/dev/topics/db/models/#id6

class Section(models.Model):
    survey = models.ForeignKey(Survey)
    name = models.CharField(max_length=100)

    class Meta:
        abstract = True        # no db table created for this model

    def __unicode__(self):
        return self.name


class RatingSection(Section):
    pass

class MultiChoiceSection(Section):
    can_select_multiple = models.BooleanField()
lazerscience
how exactly would I implement that "type" attribute though. Ideally i'd like it to simply list all the subclasses of Section so I don't have to change type handling manually.
Adam M-W
Well if you take my solution from above you just need to register a ModelAdmin for evey Section class, and then you'd have a seperate Section in the admin to edit instances of this class!Or second solution: You have one class with an attribute `choices` that can either be "rating" or "multichoice", see http://www.djangoproject.com/documentation/models/choices/ on how to implement field choices!
lazerscience