Advertisement
Guest User

Untitled

a guest
Sep 5th, 2019
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.75 KB | None | 0 0
  1. from django.db import models
  2.  
  3. # Create your models here.
  4.  
  5.  
  6. class Genre(models.Model):
  7.     """
  8.    Model representing a book genre (e.g. Science Fiction, Non Fiction).
  9.    """
  10.     name = models.CharField(
  11.         max_length=200, help_text="Enter a book genre (e.g. Science Fiction, French Poetry etc.)")
  12.  
  13.     def __str__(self):
  14.         """
  15.        String for representing the Model object (in Admin site etc.)
  16.        """
  17.         return self.name
  18.  
  19.  
  20. class Book(models.Model):
  21.     """
  22.    Model representing a book (but not a specific copy of a book).
  23.    """
  24.     title = models.CharField(max_length=200)
  25.     author = models.ForeignKey('Author', on_delete=models.SET_NULL, null=True)
  26.     # Foreign Key used because book can only have one author, but authors can have multiple books
  27.     # Author as a string rather than object because it hasn't been declared yet in the file.
  28.     summary = models.TextField(
  29.         max_length=1000, help_text="Enter a brief description of the book")
  30.     isbn = models.CharField(
  31.         'ISBN', max_length=13, help_text='13 Character <a href="https://www.isbn-international.org/content/what-isbn">ISBN number</a>')
  32.     genre = models.ManyToManyField(
  33.         Genre, help_text="Select a genre for this book")
  34.     genre = models.ManyToManyField(Genre, help_text="Select a genre for this book")
  35.     # ManyToManyField used because a genre can contain many books and a Book can cover many genres.
  36.     # Genre class has already been defined so we can specify the object above.
  37.  
  38.     def display_genre(self):
  39.         """
  40.        Creates a string for the Genre. This is required to display genre in Admin.
  41.        """
  42.         return ', '.join([genre.name for genre in self.genre.all()[:3]])
  43.     display_genre.short_description = 'Genre'
  44.  
  45.     def __str__(self):
  46.         """
  47.        String for representing the Model object.
  48.        """
  49.         return self.title
  50.  
  51.     def get_absolute_url(self):
  52.         """
  53.        Returns the url to access a particular book instance.
  54.        """
  55.         return reverse('book-detail', args=[str(self.id)])
  56.  
  57.  
  58. import uuid  # Required for unique book instances
  59.  
  60.  
  61. class BookInstance(models.Model):
  62.     """
  63.    Model representing a specific copy of a book (i.e. that can be borrowed from the library).
  64.    """
  65.     id = models.UUIDField(primary_key=True, default=uuid.uuid4,
  66.                           help_text="Unique ID for this particular book across whole library")
  67.     book = models.ForeignKey('Book', on_delete=models.SET_NULL, null=True)
  68.     imprint = models.CharField(max_length=200)
  69.     due_back = models.DateField(null=True, blank=True)
  70.  
  71.     LOAN_STATUS = (
  72.         ('m', 'Maintenance'),
  73.         ('o', 'On loan'),
  74.         ('a', 'Available'),
  75.         ('r', 'Reserved'),
  76.     )
  77.  
  78.     status = models.CharField(max_length=1, choices=LOAN_STATUS, blank=True,
  79.                               default='m', help_text='Book availability')
  80.  
  81.     class Meta:
  82.         ordering = ["due_back"]
  83.  
  84.     def __str__(self):
  85.         """
  86.        String for representing the Model object
  87.        """
  88.         return '%s (%s)' % (self.id, self.book.title)
  89.  
  90.  
  91. class Author(models.Model):
  92.     """
  93.    Model representing an author.
  94.    """
  95.     first_name = models.CharField(max_length=100)
  96.     last_name = models.CharField(max_length=100)
  97.     date_of_birth = models.DateField(null=True, blank=True)
  98.     date_of_death = models.DateField('Died', null=True, blank=True)
  99.  
  100.     def get_absolute_url(self):
  101.         """
  102.        Returns the url to access a particular author instance.
  103.        """
  104.         return reverse('author-detail', args=[str(self.id)])
  105.  
  106.     def __str__(self):
  107.         """
  108.        String for representing the Model object.
  109.        """
  110.         return '%s, %s' % (self.last_name, self.first_name)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement