开发者

How to organize news/ratings/comments database?

My site will have news with possibility to rate them and leave comments. Each user will be able to rate one news only once and leave only one comment. At the same time, I should know which user rated the news and who left the comment.

How to organize such database?

I think about the following structure:

class News(db.Model):
    news = db.TextProperty()
    added = db.DateTimeProperty(auto_now_add=True)
    rating = db.Reference开发者_如何学GoProperty(NewsRatings)
    comments = db.ReferenceProperty(NewsComments)
    added = db.DateTimeProperty(auto_now_add=True)

class NewsRatings(db.Model):
    user = db.ReferenceProperty(Users)
    rating = db.IntegerProperty()
    added = db.DateTimeProperty(auto_now_add=True)

class NewsComments(db.Model):
    user = db.ReferenceProperty(Users)
    comment = db.TextProperty()
    added = db.DateTimeProperty(auto_now_add=True)

class Users(db.Model):
    user = db.IntegerProperty()

Is it correct approach? Will I know who left particular comment for particular news?


Your current model only allows for each news item to have a single rating and a single comment (each of which could belong to an arbitrary number of news items). Instead, put the ReferenceProperty on NewsRatings and NewsComments, referencing the News item to which they belong.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜