2016-01-27 10 views
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) 

其中I存储用户喜欢的物品的表:

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) 

现在,在这里,表格喜欢和不喜欢,在结构上是相同的。 我发现它更好地存储它像这样而不是存储称为is_liked布尔字段,因为我完全知道我存储什么数据。所以当我知道我只对LikedArticle感兴趣时,我不必查询大量文章。 这是正确的方法吗?我只是感到困惑,因为它们在结构上看起来一样,而且对这种设计感觉不太对。

回答

0

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

1

我推荐的最佳方法是使用一个表并添加is_liked字段。 (并添加索引到这个领域,所以你得到高性能查询)

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

使用一个有各个领域的抽象模型等,并与表从抽象模型继承

class ActionOnArticle(Model): 

    your fields here.. 

    class Meta: 
     abstract = True 

class LikedArticle(ActionOnArticle): 


class UnLikedArticle(ActionOnArticle): 
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的回答很好

相关问题