0

我有一张Article桌子

class Article(models.Model):
    """
    Model to keep articles
    """

    ext_id = models.UUIDField(primary_key=True, db_index=True, default=uuid.uuid4, editable=False)
    title = models.CharField(max_length=255, unique=True, db_index=True)
    content = models.TextField()
    summary = models.TextField()
    img_url = models.URLField(max_length=200)
    author = models.CharField(max_length=50, blank=True, null=True)
    sport  = models.ForeignKey('Sport')
    posted_on= models.DateTimeField()
    created = models.DateTimeField(auto_now_add=True)
    updated = models.DateTimeField(auto_now=True)

    def __unicode__(self):
        return "%s by %s" % (self.title, self.author)

我存储用户喜欢的文章的表格:

class LikedArticle(models.Model):

    """
    Articles that a user wants to read 
    """

    ext_id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    article = models.ForeignKey(Article)
    profile = models.ForeignKey(Profile)
    created = models.DateTimeField(auto_now_add=True)
    updated = models.DateTimeField(auto_now=True)

并且不喜欢:

class UnlikedLikedArticle(models.Model):

    """
    Articles that a user does not want to read 
    """

    ext_id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    article = models.ForeignKey(Article)
    profile = models.ForeignKey(Profile)
    created = models.DateTimeField(auto_now_add=True)
    updated = models.DateTimeField(auto_now=True)

现在在这里,喜欢和不喜欢的表在结构上是相同的。我发现像这样存储它而不是存储一个 bool 字段更好,is_liked因为我完全知道我正在存储什么数据。所以当我知道我只对LikedArticle. 这是正确的方法吗?我只是感到困惑,因为它们在结构上看起来相同,而且这种设计感觉有些不对劲。

4

3 回答 3

1

如果您想为每个配置文件保存其他信息,我认为 is_liked 不是一个好选择,例如:谁喜欢什么以及何时等等。如果您想丢失这些信息,那么我的建议是使用多对多关系,并且文章模型将是这样的:

class Article(models.Model):
   """
    Model to keep articles
   """

    ext_id = models.UUIDField(primary_key=True, db_index=True, default=uuid.uuid4, editable=False)
    title = models.CharField(max_length=255, unique=True, db_index=True)
    content = models.TextField()
    summary = models.TextField()
    img_url = models.URLField(max_length=200)
    author = models.CharField(max_length=50, blank=True, null=True)
    sport  = models.ForeignKey('Sport')
    posted_on= models.DateTimeField()
    created = models.DateTimeField(auto_now_add=True)
    updated = models.DateTimeField(auto_now=True)
    likes = models.ManyToManyField(Profile)
    unlikes = models.ManyToManyField(Profile)

def __unicode__(self):
    return "%s by %s" % (self.title, self.author)

https://docs.djangoproject.com/en/1.8/topics/db/examples/many_to_many/

如果您想保存我回复开头提到的信息,我认为@Eyal 的回答很好

于 2016-01-27T14:24:43.443 回答
1

我推荐的最佳方法是使用一个表并添加is_liked字段。(并向该字段添加索引,以便获得高性能查询)

但如果您仍然想使用 2 表的方法,那么您需要修复您的设计。

使用一个包含所有字段的抽象模型,并且 Like 和 Like 表继承自抽象模型

class ActionOnArticle(Model):

    your fields here.. 

    class Meta:
        abstract = True

class LikedArticle(ActionOnArticle):


class UnLikedArticle(ActionOnArticle):
于 2016-01-27T14:05:13.180 回答
0

我会使用“is_liked”布尔字段并对其进行过滤以获取喜欢或不喜欢的文章。对 BooleanField 进行过滤(在字段选项上添加 db_index=True)对于任何体面的数据库都将非常快,因此即使它们很大,使用单独的表也不太可能显着提高性能。

于 2016-01-27T14:03:44.330 回答