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

all Stages completed #15

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open

Conversation

mihir02badkur
Copy link
Member

@mihir02badkur mihir02badkur commented Jul 22, 2021

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.

book_reviewed = models.ForeignKey(Book, on_delete=models.CASCADE)
rating = models.IntegerField(
default=1,
validators=[MaxValueValidator(10), MinValueValidator(0)]
Copy link
Member

Choose a reason for hiding this comment

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

Good use of validators in Django.

book_id = request.POST.get('bid')
rateing = float(request.POST.get('brate'))
list_book = Review.objects.filter(book_reviewed=Book.objects.get(id__exact=book_id))
list_user_book = Review.objects.filter(Q(book_reviewed=Book.objects.get(id__exact=book_id)) & Q(reviewer=request.user))
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 not wrong but Q is not needed for such simple queries. You can write even very much complex queries without using Q.

c.save()
b = Book.objects.get(id__exact=book_id)
previous_rating_of_book = b.rating
new_rating = ((previous_rating_of_book*len(list_book))-previous_rating_by_user+rateing)/len(list_book)
Copy link
Member

Choose a reason for hiding this comment

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

This is correct but a more Django-ish way for this would be using aggregate funtion in Django ORM:

Review.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