News

which is the better design of database for article Like function?

I’m using Django to develop a news website.Now I’m developing the like function in news detail page, this function is like that at the bottom of the news, there is a like button, once you click it the amount of like will add 1.

Now I have 2 choices to design the table of the database in django we call it model.I’m not sure which design is better.

First:just use one News model

class News(models.Model):
    ...code..
    is_up = models.BooleanField(default=True)
    up_count = models.IntegerField(default=0)

Once is_up ,up_count will add 1.

Sceond:use 2 models,News model and a separate Like model

class News(models.Model):
    ...code..
    up_count = models.IntegerField(default=0)

class Like(models.Model):
    """
    点赞表
    """
    nid = models.AutoField(primary_key=True)
    user = models.ForeignKey('UserInfo', null=True, on_delete=models.CASCADE)
    news = models.ForeignKey("News", null=True, on_delete=models.CASCADE)
    is_up = models.BooleanField(default=True)

    class Meta:
        unique_together = [
            ('news', 'user'),
        ]

Any friend can tell which design is better ?

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload the CAPTCHA.