简体   繁体   English

子类的Django模型/数据库设计

[英]Django Model/Database design for subclasses

Ok, i'm shit at describing. 好吧,我很讨厌描述。 Here's a relationship diag. 这是一个关系诊断。 关系图

In Django i've made my models like: 在Django中,我的模型如下:

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. 问题是我几乎可以肯定这不是正确的方法,即使是这样,我也无法弄清楚如何允许Django中的admin区域向用户显示选择内容,询问用户什么子类型他们想要的部分。

What would be the best way to structure models of this sort? 构造这种模型的最佳方法是什么?

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. 您也可以有一个节类,其属性type可以是等级或多项选择-将在管理员中呈现,然后作为选择框呈现。
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 但我认为您应该看看Django创建抽象模型的可能性: 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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM