在 django 中设计通知系统的适当模式?



我正在尝试在Django中创建一个通知系统。对不同的表(如注释表、关系表(有一个notification_type字段、user_id和一个外键更好吗?

或者除了它们都有一个user_id字段之外,它们是否应该为每种类型的通知(如"喜欢通知"、"评论通知"等(都有一个单独的表?

设计1:


class LikeNotification(models.Model):
post = models.ForeignKey(Post, on_delete=models.CASCADE)
user = models.ForeignKey(User, on_delete=models.CASCADE)
class CommentNotification(models.Model):
post = models.ForeignKey(Post, on_delete=models.CASCADE)
user = models.ForeignKey(User, on_delete=models.CASCADE)
class PostNotification(models.Model):
post = models.ForeignKey(Post, on_delete=models.CASCADE)
user = models.ForeignKey(User, on_delete=models.CASCADE)

class BondNotification(models.Model):
bond = models.ForeignKey(Bond, on_delete=models.CASCADE,)
user = models.ForeignKey(User, on_delete=models.CASCADE)

设计2:


class Notification(models.Model):
notification_type = models.IntegerField()
user = models.ForeignKey(User, on_delete=models.CASCADE)
like = models.ForeignKey(Like, on_delete=models.CASCADE)
comment = models.ForeignKey(Comment, on_delete=models.CASCADE)
bond = models.ForeignKey(Bond, on_delete=models.CASCADE)
post = models.ForeignKey(Post, on_delete=models.CASCADE)

使用ContentType

content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE)
object_id = models.PositiveIntegerField()
content_object = GenericForeignKey('content_type', 'object_id')

这样,您可以Foreignkey任何模型

最新更新