简体   繁体   English

使用Django模型系统进行评分

[英]Rating using Django Model System

class User(models.Model):
    username = models.CharField(max_length=150)
    email = models.EmailField(max_length=150)

class Recipes(models.Model):
    title = models.CharField(max_length=150)
    user = models.ForeignKey(User, related_name='user', on_delete=models.CASCADE, null=True)
    created = models.DateTimeField(auto_now=True, auto_now_add=False)
    updated = models.DateTimeField(auto_now=False, auto_now_add=True)

class FavoriteRecipes(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE, null=True)
    recipe = models.ForeignKey(Recipes, on_delete=models.CASCADE, null=True)

class FavoriteChef(models.Model): 
    user = models.ForeignKey(User, related_name='user', on_delete=models.CASCADE)
    favorite = models.ForeignKey(User, related_name='favorite', on_delete=models.CASCADE)

I am trying to create a rating system to recipes and users. 我正在尝试为食谱和用户创建一个评分系统。 I am not sure if this is the best approach or not, but his are my questions: 我不确定这是否是最好的方法,但是他是我的问题:

  1. how I count the ammount of recipes liked by different user, example: 我如何计算不同用户喜欢的食谱数量,例如:

    • user 1 liked recipe 1 使用者1喜欢食谱1
    • user 2 liked recipe 1 使用者2喜欢食谱1
    • recipe 1 is liekd by 2. 配方1等于2。
  2. how to limit the user from voting may times using this approach, since django generates its own pk entries like this can exists: 由于django会生成自己的pk条目,因此如何使用这种方法限制用户投票可能会存在:

    • pk:1 user 1 liked recipe 1 pk:1用户1喜欢食谱1

    • pk:2 user 1 liked recipe 1 pk:2用户1喜欢食谱1

and thanks 谢谢

you could limit the number of votes with: 您可以通过以下方式限制投票数:

if FavoriteRecipes.objects.filter(recipe=Recipes, user=User).exists():
  #do not update
else:
    #save vote

then you could count the votes with: 那么您可以使用以下方法计算票数:

count= FavoriteRecipes.objects.filter(recipe=Recipe).count()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM