Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Completed All Tasks #13

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

ayushigupta931
Copy link
Member

CSoC Task 2 Submission

I have completed the following tasks

  • Stage 1
  • Stage 2
  • Stage 3
  • Stage 4

Copy link
Member

@m-e-l-u-h-a-n m-e-l-u-h-a-n left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work on the task 🎉 . Your submission is evaluated.


class RatingOfBook(models.Model):
book = models.ForeignKey(Book, on_delete=models.CASCADE)
ratingValue = models.PositiveIntegerField(null=True, blank=True)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can also use MinValueValidator and MaxValueValidators here to have database level constraints set on this field.

response_data['message'] = "success"
bookId = request.POST.get('bookid')
rating = request.POST.get('ratingOfTheBook')
BookWhichIsRated = Book.objects.get(id = bookId)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PascalCase have different conventions and meanings in Django and in general in python. These are use mostly in class based names or for literal names. snake_case is generally used for naming variables in python.

Comment on lines +209 to +218
Ratings = [x for x in RatingOfBook.objects.filter(book = BookWhichIsRated)]
l = len(Ratings)
sum = 0
for r in Ratings:
sum = sum + r.ratingValue

average_rating = sum/l
BookWhichIsRated.rating = average_rating
BookWhichIsRated.save()

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While this is correct , a more clean and Django-ish way for doing this could be:
It uses aggeregate function in Django. You can read more about it in their documentation.

average_rating = RatingOfBook.objects.filter(book__pk=book_id).aggregate(Avg('rating'))['rating__avg']

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants