Advertisement
horozov86

Lab: Working with Queries in Django

Nov 9th, 2023 (edited)
513
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.24 KB | None | 0 0
  1. # 1.    Books Finder
  2.  
  3. def find_books_by_genre_and_language(book_genre, book_language):
  4.     found_books = Book.objects.filter(genre=book_genre, language=book_language)
  5.    
  6.     return found_books
  7.    
  8. # print(find_books_by_genre_and_language("Romance", "English"))
  9. # print(find_books_by_genre_and_language("Poetry", "Spanish"))
  10. # print(find_books_by_genre_and_language("Mystery", "English"))
  11.  
  12.  
  13.  
  14. # 2.    Find Authors' Nationalities
  15.  
  16. def find_authors_nationalities():
  17.     authors = Author.objects.exclude("nationality"=None)
  18.    
  19.     result = [
  20.         f"{author.first_name} {author.last_name} is {author.nationality}" for author in authors
  21.     ]
  22.    
  23.     return "\n".join(result)
  24.    
  25. # print(find_authors_nationalities())
  26.  
  27.  
  28. # 3.    Order Books by Year
  29.  
  30. def order_books_by_year():
  31.     ordered_books = Book.objects.order_by("publication_year", "title")
  32.    
  33.     result = [
  34.         f"{book.publication_year} year: {book.title} by {book.author}" for book in ordered_books
  35.     ]
  36.    
  37.    
  38.     return "\n".join(result)
  39.    
  40. # print(order_books_by_year())
  41.  
  42.  
  43. # 4.    Delete Review by ID
  44.  
  45. def delete_review_by_id(review_id):
  46.     review = Review.objects.get(id=review_id)
  47.     review.delete()
  48.    
  49.     return f"Review by {review.reviewer_name} was deleted"
  50.    
  51. # print(delete_review_by_id(4))
  52. # print(delete_review_by_id(1))
  53. # print(delete_review_by_id(8))
  54.  
  55.  
  56. # 5.    Filter Authors by Nationalities
  57.  
  58. def filter_authors_by_nationalities(nationality):
  59.     authors = Author.objects.filter(nationality=nationality).order_by("first_name", "last_name")
  60.    
  61.     result = [
  62.         author.biography if author.biography is not None else f"{author.first_name} {author.last_name}" for author in authors
  63.     ]
  64.    
  65.     return "\n".join(authors)
  66.    
  67. # print("American authors:")
  68. # print(filter_authors_by_nationalities('American'))
  69. # print()
  70. # print("British authors:")
  71. # print(filter_authors_by_nationalities('British'))
  72. # print()
  73. # print("Authors with no nationalities:")
  74. # print(filter_authors_by_nationalities(None))
  75.  
  76.  
  77. # 6.    Filter Authors by Birth Year
  78.  
  79. def filter_authors_by_birth_year(first_year, second_year):
  80.     authors = Author.objects.filter(birth_date__year__range=(first_year, last_name)).order_by("-birth_date"))
  81.    
  82.     result = [
  83.         f"{author.birth_date}: {author.first_name} {author.last_name}" for author in authors
  84.     ]
  85.    
  86.    
  87.     return "\n".join(result)
  88.    
  89. # print("Authors born between 1980 and 2000:")
  90. # print(filter_authors_by_birth_year(1980, 2000))
  91. # print()
  92. # print("Authors born between 1950 and 1960:")
  93. # print(filter_authors_by_birth_year(1950, 1960))
  94. # print()
  95. # print("Authors born between 2000 and 2010:")
  96. # print(filter_authors_by_birth_year(2000, 2010))
  97.  
  98. # 7.    Change Reviewer's Name
  99.  
  100. def change_reviewer_name(reviewer_name, new_name):
  101.     (Review.objects
  102.     .filter(reviewer_name=reviewer_name)
  103.     .update(reviewer_name=new_name))
  104.     result = Review.objects.all()
  105.     return result
  106.    
  107. # print("Change Alice Johnson to A.J.:")
  108. # print(change_reviewer_name("Alice Johnson", "A.J."))
  109. # print()
  110. # print("Change Bob Wilson to Bobby W.:")
  111. # print(change_reviewer_name("Bob Wilson", "Bobby W."))
  112. # print()
  113. # print("Change A.J. to A. Johnson:")
  114. # print(change_reviewer_name("A.J.", "A. Johnson"))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement