子类的 Django 模型/数据库设计
在 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.
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您也可以有一个部分类,其属性
type
可以是评级或多选 - 它将在管理中呈现为选择框。但我认为你应该看看 Django 创建抽象模型的可能性: http://docs.djangoproject.com/en/dev/topics/db/models/#id6
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