Skip to content
Branch: master
Find file Copy path
Find file Copy path
Vladyslav Hnatchenko fixed according to PEP8 6f12e64 Feb 1, 2019
4 contributors

Users who have contributed to this file

@hamishwillee @lmeerwood @deSagaz @AlekseiMarinichenko
121 lines (91 sloc) 4.49 KB
from django.db import models
# Create your models here.
from django.urls import reverse # To generate URLS by reversing URL patterns
class Genre(models.Model):
"""Model representing a book genre (e.g. Science Fiction, Non Fiction)."""
name = models.CharField(
help_text="Enter a book genre (e.g. Science Fiction, French Poetry etc.)"
def __str__(self):
"""String for representing the Model object (in Admin site etc.)"""
class Language(models.Model):
"""Model representing a Language (e.g. English, French, Japanese, etc.)"""
name = models.CharField(max_length=200,
help_text="Enter the book's natural language (e.g. English, French, Japanese etc.)")
def __str__(self):
"""String for representing the Model object (in Admin site etc.)"""
class Book(models.Model):
"""Model representing a book (but not a specific copy of a book)."""
title = models.CharField(max_length=200)
author = models.ForeignKey('Author', on_delete=models.SET_NULL, null=True)
# 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 file.
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="'
'">ISBN number</a>')
genre = models.ManyToManyField(Genre, help_text="Select a genre for this book")
# ManyToManyField used because a genre can contain many books and a Book can cover many genres.
# Genre class has already been defined so we can specify the object above.
language = models.ForeignKey('Language', on_delete=models.SET_NULL, null=True)
def display_genre(self):
"""Creates a string for the Genre. This is required to display genre in Admin."""
return ', '.join([ for genre in self.genre.all()[:3]])
display_genre.short_description = 'Genre'
def get_absolute_url(self):
"""Returns the url to access a particular book instance."""
return reverse('book-detail', args=[str(])
def __str__(self):
"""String for representing the Model object."""
return self.title
import uuid # Required for unique book instances
from datetime import date
from django.contrib.auth.models import User # Required to assign User as a borrower
class BookInstance(models.Model):
"""Model representing a specific copy of a book (i.e. that can be borrowed from the library)."""
id = models.UUIDField(primary_key=True, default=uuid.uuid4,
help_text="Unique ID for this particular book across whole library")
book = models.ForeignKey('Book', on_delete=models.SET_NULL, null=True)
imprint = models.CharField(max_length=200)
due_back = models.DateField(null=True, blank=True)
borrower = models.ForeignKey(User, on_delete=models.SET_NULL, null=True, blank=True)
def is_overdue(self):
if self.due_back and > self.due_back:
return True
return False
('d', 'Maintenance'),
('o', 'On loan'),
('a', 'Available'),
('r', 'Reserved'),
status = models.CharField(
help_text='Book availability')
class Meta:
ordering = ['due_back']
permissions = (("can_mark_returned", "Set book as returned"),)
def __str__(self):
"""String for representing the Model object."""
return '{0} ({1})'.format(,
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(])
def __str__(self):
"""String for representing the Model object."""
return '{0}, {1}'.format(self.last_name, self.first_name)
You can’t perform that action at this time.