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

发布于 2024-09-07 10:57:32 字数 2082 浏览 3 评论 0原文

好吧,我描述的很糟糕。这是一个关系诊断。 关系诊断

在 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

问题是我几乎可以肯定这不是正确的方法,即使是,我也无法弄清楚如何允许 Django 中的管理区域显示向用户询问他们想要什么子类型的部分的选择。

构建此类模型的最佳方法是什么?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

墨小墨 2024-09-14 10:57:32

您也可以有一个部分类,其属性 type 可以是评级或多选 - 它将在管理中呈现为选择框。
但我认为你应该看看 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()

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()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文