Erd and Models
Erd and Models
"Designing a robust database schema is essential for implementing CRUD (Create, Read, Update, Delete)
operations in the Local Library website. Please follow these steps:
Criteria
Marks
Presence of all necessary entities and relationships
2
Consistency with project requirements and constraints
2
class Genre(models.Model):
"""Model representing a book genre."""
name = models.CharField(max_length=200, help_text='Enter a book genre (e.g. Science Fiction)')
def __str__(self):
"""String for representing the Model object."""
return self.name
class Language(models.Model):
"""Model representing a language (e.g. English, French, Japanese, etc.)."""
name = models.CharField(max_length=100, help_text="Enter the book's natural language (e.g. English, French,
Japanese, etc.)")
def __str__(self):
"""String for representing the Model object."""
return self.name
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)
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>')
genre = models.ManyToManyField(Genre, help_text='Select a genre for this book')
language = models.ForeignKey('Language', on_delete=models.SET_NULL, null=True)
def __str__(self):
https://docs.google.com/document/d/1bknNJ-4HAQoP2bBgrsxWi0w-Ox3HWau3lxtAGey61Ww/edit?pli=1 1/3
4/30/24, 8:57 PM Model Question and Answer - DJANGO - Google Docs
"""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 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)
LOAN_STATUS = (
('m', 'Maintenance'),
('o', 'On loan'),
('a', 'Available'),
('r', 'Reserved'),
)
class Meta:
ordering = ['due_back']
permissions = (("can_mark_returned", "Set book as returned"),)
def __str__(self):
"""String for representing the Model object."""
return f'{self.id} ({self.book.title})'
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}'
```
In this updated version, I've added additional fields such as `Genre`, `Language`, `BookInstance`, and extended
fields like `summary`, `isbn`, and `imprint` to the `Book` and `BookInstance` models, similar to those found in the
MDN Local Library project.
Criteria
Marks
Completeness and accuracy of model fields
https://docs.google.com/document/d/1bknNJ-4HAQoP2bBgrsxWi0w-Ox3HWau3lxtAGey61Ww/edit?pli=1 2/3
4/30/24, 8:57 PM Model Question and Answer - DJANGO - Google Docs
2
Correct definition of relationships and constraints
2
Implementation of model methods (e.g., __str__)
2
Proper use of Meta class for ordering and permissions
2
https://docs.google.com/document/d/1bknNJ-4HAQoP2bBgrsxWi0w-Ox3HWau3lxtAGey61Ww/edit?pli=1 3/3