"Project object has no attribute review_set"

61 views
Skip to first unread message

Melissa Malala

unread,
Oct 12, 2018, 8:24:47 AM10/12/18
to Django users
Getting an error that says "Project object has no attribute review_set" when trying to get the mean of ratings using Numpy. HTML looks something like this, what could be the problem? Please have a look below and let us know what you think:


<h5>{{ project.review.count }} reviews ({{ 
project.average_design | floatformat }} average rating of design)</h5>
<h5>{{ project.review.count }} reviews ({{ 
project.average_content | floatformat }} average rating of content)</h5>
<h5>{{ project.review.count }} reviews ({{ project.average_usability | floatformat }} average rating of usability)</h5>
class Project(models.Model):
title = models.TextField(max_length=200, null=True, blank=True, default="title")
project_image = models.ImageField(upload_to='picture/', null=True, blank=True)
description = models.TextField()
project_url=models.URLField(max_length=250)
def average_design(self):
    design_ratings = list(map(lambda x: x.design_rating, self.review_set.all()))
    return np.mean(design_ratings)

def average_usability(self):
    usability_ratings = list(map(lambda x: x.usability_rating, self.review_set.all()))
    return np.mean(usability_ratings)

def average_content(self):
    content_ratings = list(map(lambda x: x.content_rating, self.review_set.all()))
    return np.mean(content_ratings)
class Review(models.Model):
RATING_CHOICES = (
    (1, '1'),
    (2, '2'),
    (3, '3'),
    (4, '4'),
    (5, '5'),
    (6, '6'),
    (7, '7'),
    (8, '8'),
    (9, '9'),
    (10, '10'),

)
project = models.ForeignKey(Project, null=True, blank=True, on_delete=models.CASCADE, related_name="review")
user = models.ForeignKey(User, null=True, blank=True, on_delete=models.CASCADE, related_name='user')
image = models.ForeignKey(Image, on_delete=models.CASCADE, related_name="project", null=True, blank=True)
comment = models.TextField()
design_rating = models.IntegerField(choices=RATING_CHOICES, null=True, blank=True)
usability_rating = models.IntegerField(choices=RATING_CHOICES, null=True, blank=True)
content_rating = models.IntegerField(choices=RATING_CHOICES, null=True, blank=True)
 
 

Vijay Khemlani

unread,
Oct 12, 2018, 9:01:42 AM10/12/18
to django...@googlegroups.com
Since the related name in the project foreign key is "review" you have to use that keyword for the related query from project, so it should be "self.review.all()" instead of "self.review_set.all()" in your Project methods.

Regards!

Nelson Varela

unread,
Oct 12, 2018, 9:24:34 AM10/12/18
to Django users
Use the .review you've defined as related name. The _set is when you don't have a related name.
Reply all
Reply to author
Forward
0 new messages