简体   繁体   English

Django评论网站的数据库设计

[英]Database design for Django review website

Hi I'm developing a review site for learning Django. 嗨,我正在开发一个学习Django的评论网站。

Every review has a list of rating attributes like: 每个评论都有一个评分属性列表,例如:

  • Price: [x] [x] [x] [x] [x] 价格: [x] [x] [x] [x] [x]
  • Location: [x] [x] [ ] [ ] [ ] 位置: [x] [x] [] [] []
  • Comfort: [x] [x] [x] [ ] [ ] 舒适度: [x] [x] [x] [] []
  • etc. 等等

I'm stuck at the point of writing the review/rating model. 我被困在编写评论/评分模型的时候。 How can I describe this with MVC pattern? 我如何用MVC模式描述这一点? The list of rating attributes can be different. 评级属性列表可以不同。

Currently I've this code: 目前,我有以下代码:

class Place(models.Model):
    name = models.CharField(max_length=512)
    address = models.CharField(max_length=512)
    author = models.ForeignKey(UserProfile)

class Review(models.Model):
    ????

class Review(models.Model):
    name = models.CharField(max_length=1024)
    text = models.TextField()
    author = models.ForeignKey(UserProfile)
    place = models.ForeignKey(place)
    ratings = models.ForeignKey(Rating)

Any hint? 有什么提示吗?

If the list of (potential) attributes is available beforehand, you can just introduce a database field for each of them and store not-set attributes as None : 如果(潜在)属性列表事先可用,则可以为每个属性引入一个数据库字段,并将未设置的属性存储为None

class Review(models.Model):
    name = models.CharField(max_length=1024)
    text = models.TextField()
    author = models.ForeignKey(UserProfile)
    place = models.ForeignKey(place)

    price_rating = models.IntegerField(null=True)
    location_rating = models.IntegerField(null=True)
    # ...

Otherwise (if the attributes/ratings are totally variable), you have to create another model to store your attributes (drop the _rating fields above then): 否则(如果属性/等级完全可变),则必须创建另一个模型来存储属性(然后在上方删除_rating字段):

class ReviewAttribute(models.Model):
    review = models.ForeignKey(Review, related_name='attributes')
    name = models.CharField(max_length=100)
    value = models.IntegerField()

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

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