-
Notifications
You must be signed in to change notification settings - Fork 41
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
base: master
Are you sure you want to change the base?
Conversation
There was a problem hiding this 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) |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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.
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() | ||
|
There was a problem hiding this comment.
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']
CSoC Task 2 Submission
I have completed the following tasks