0% found this document useful (0 votes)
32 views3 pages

Erd and Models

The document discusses designing a database schema for a local library website using Django. It provides steps to generate an entity-relationship diagram and write Django model classes to represent the entities and support CRUD operations. Model classes are defined for Book, BookInstance, Genre, Language, and Author with appropriate fields and relationships.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views3 pages

Erd and Models

The document discusses designing a database schema for a local library website using Django. It provides steps to generate an entity-relationship diagram and write Django model classes to represent the entities and support CRUD operations. Model classes are defined for Book, BookInstance, Genre, Language, and Author with appropriate fields and relationships.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

4/30/24, 8:57 PM Model Question and Answer - DJANGO - Google Docs

"Designing a robust database schema is essential for implementing CRUD (Create, Read, Update, Delete)
operations in the Local Library website. Please follow these steps:

1. **Generate an Entity-Relationship Diagram (ERD):** (4 marks )


- Create an ERD illustrating the relationships between key entities in the Local Library system, such as books,
authors, users, transactions, etc. Include cardinality and multiplicity annotations to depict the relationships accurately.

2. **Write Models for CRUD Operations:** (8 Marks)


- Based on the ERD, write Django model classes to represent each entity in the Local Library system. Ensure that
the models include necessary fields and relationships to support CRUD operations effectively.

1. Entity-Relationship Diagram (ERD):


● Below is the Entity-Relationship Diagram (ERD) for the Local Library system:
Draw the diagram
Scheme of evaluation

Criteria
Marks
Presence of all necessary entities and relationships
2
Consistency with project requirements and constraints
2

1. Below is the model classes mentioned in the ERD

from django.db import models


from django.urls import reverse
import uuid # Required for unique book instances

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'),
)

status = models.CharField(max_length=1, choices=LOAN_STATUS, blank=True, default='m', 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 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.

Scheme for evaluation

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

You might also like