Reputation: 65
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:
how I count the ammount of recipes liked by different user, example:
how to limit the user from voting may times using this approach, since django generates its own pk entries like this can exists:
pk:1 user 1 liked recipe 1
pk:2 user 1 liked recipe 1
and thanks
Upvotes: 0
Views: 664
Reputation: 36
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()
Upvotes: 1