Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #view
- class AuthorDetailView(generic.DetailView):
- model= Author
- def get_context_data(self, **kwargs):
- # Call the base implementation first to get the context
- context = super(AuthorDetailView, self).get_context_data(**kwargs)
- # Create any data and add it to the context
- context['books']=Book.objects.all()
- return context
- #model
- class Book(models.Model):
- """Model representing a book (but not a specific copy of a book)."""
- title = models.CharField(max_length=200)
- # Foreign Key used because book can only have one author, but authors can have multiple books
- # Author as a string rather than object because it hasn't been declared yet in the file
- author = models.ForeignKey('Author', on_delete=models.SET_NULL, null=True)
- summary = models.TextField(max_length=1000, help_text='Enter a brief description of the book')
- isbn = models.CharField('ISBN', max_length=13, help_text='13 Character <a href="https://www.isbn-international.org/content/what-isbn">ISBN number</a>')
- # ManyToManyField used because genre can contain many books. Books can cover many genres.
- # Genre class has already been defined so we can specify the object above.
- genre = models.ManyToManyField(Genre, help_text='Select a genre for this book')
- def __str__(self):
- """String for representing the Model object."""
- return self.title
- def get_absolute_url(self):
- """Returns the url to access a detail record for this book."""
- return reverse('book-detail', args=[str(self.id)])
- class Author(models.Model):
- """Model representing an author."""
- first_name = models.CharField(max_length=100)
- last_name = models.CharField(max_length=100)
- date_of_birth = models.DateField(null=True, blank=True)
- date_of_death = models.DateField('Died', null=True, blank=True)
- class Meta:
- ordering = ['last_name', 'first_name']
- def get_absolute_url(self):
- """Returns the url to access a particular author instance."""
- return reverse('author-detail', args=[str(self.id)])
- def __str__(self):
- """String for representing the Model object."""
- return f'{self.last_name} {self.first_name}'
Add Comment
Please, Sign In to add comment