-
Notifications
You must be signed in to change notification settings - Fork 42
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
All tasks done #14
base: master
Are you sure you want to change the base?
All tasks done #14
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 assignment 🎉 . Your submission is evaluated.
Calc_Rate=BookRating.objects.filter(book=books) | ||
book_rate = 0 | ||
for r in Calc_Rate: | ||
book_rate += r.rating | ||
book_rate /= len(Calc_Rate) | ||
book_rate = round(book_rate,2) |
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.
A very clean and more Django-ish way for this could be, using aggregate functions in Django ORM.
book_rate = BookRating.objects.filter(book__pk=book_id).aggregate(Avg('rating'))['rating__avg']
rating = models.IntegerField(default=0, validators=[ | ||
MaxValueValidator(10), | ||
MinValueValidator(0) | ||
]) | ||
user = models.ForeignKey(User, related_name='user', null=True, blank=True, on_delete=models.SET_NULL) |
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 use of validators here.
books = Book.objects.get(id=book_id) | ||
rating1 = BookRating.objects.filter(book=books, user=request.user) | ||
|
||
if len(rating1)==0: | ||
new_rating = BookRating(book=books, user=request.user, rating=book_rate) |
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 get_or_create
could be used here for better code clarity.
CSoC Task 2 Submission
I have completed the following tasks