Thesummer

Specific_Author_1

Oct 22nd, 2019
555
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.24 KB | None | 0 0
  1. #view
  2. class AuthorDetailView(generic.DetailView):
  3.     model= Author
  4.     def get_context_data(self, **kwargs):
  5.         # Call the base implementation first to get the context
  6.         context = super(AuthorDetailView, self).get_context_data(**kwargs)
  7.         # Create any data and add it to the context
  8.         context['books']=Book.objects.all()
  9.         return context
  10.  
  11. #model
  12. class Book(models.Model):
  13.     """Model representing a book (but not a specific copy of a book)."""
  14.     title = models.CharField(max_length=200)
  15.  
  16.     # Foreign Key used because book can only have one author, but authors can have multiple books
  17.     # Author as a string rather than object because it hasn't been declared yet in the file
  18.     author = models.ForeignKey('Author', on_delete=models.SET_NULL, null=True)
  19.    
  20.     summary = models.TextField(max_length=1000, help_text='Enter a brief description of the book')
  21.     isbn = models.CharField('ISBN', max_length=13, help_text='13 Character <a href="https://www.isbn-international.org/content/what-isbn">ISBN number</a>')
  22.    
  23.     # ManyToManyField used because genre can contain many books. Books can cover many genres.
  24.     # Genre class has already been defined so we can specify the object above.
  25.     genre = models.ManyToManyField(Genre, help_text='Select a genre for this book')
  26.    
  27.     def __str__(self):
  28.         """String for representing the Model object."""
  29.         return self.title
  30.    
  31.     def get_absolute_url(self):
  32.         """Returns the url to access a detail record for this book."""
  33.         return reverse('book-detail', args=[str(self.id)])
  34.  
  35. class Author(models.Model):
  36.     """Model representing an author."""
  37.     first_name = models.CharField(max_length=100)
  38.     last_name = models.CharField(max_length=100)
  39.     date_of_birth = models.DateField(null=True, blank=True)
  40.     date_of_death = models.DateField('Died', null=True, blank=True)
  41.  
  42.     class Meta:
  43.         ordering = ['last_name', 'first_name']
  44.  
  45.     def get_absolute_url(self):
  46.         """Returns the url to access a particular author instance."""
  47.         return reverse('author-detail', args=[str(self.id)])
  48.  
  49.     def __str__(self):
  50.         """String for representing the Model object."""
  51.         return f'{self.last_name} {self.first_name}'
Add Comment
Please, Sign In to add comment