Newbie here, forgive me if it's an easily solveable question.
So, I have a UserProfile model(one to one field related to base user) and posts and comments model.
Both posts and comments are related to their author by a foreign key. Both posts and comments have rating attribute(Int). For this example I'll talk about comments. My question is, how do I get all comment objects related to user by foreign key, and is it possible to not retrieve all comments themselves, but to retrieve only their rating values?
My UserProfile model looks like this
class UserProfile(models.Model):
user = models.OneToOneField(
User,
on_delete=models.CASCADE,
primary_key=True,
)
birthdate = models.DateField
post_rating = models.IntegerField(default=0)
comment_rating = models.IntegerField(default=0)
def update_post_rating(self):
pass
def update_comment_rating(self):
pass
My Comment model looks like this
class Comment(models.Model):
author = models.ForeignKey(User, on_delete=models.SET('DELETED'))
post = models.ForeignKey(Post, on_delete=models.CASCADE)
rating = models.IntegerField(default=0)
content = models.TextField
date = models.DateField(auto_now_add=True)
def like(self):
self.rating = + 1
self.save()
def dislike(self):
self.rating = - 1
self.save()