Commit d66fda38 authored by Ysabella Panghulan's avatar Ysabella Panghulan

edited Books class in models.py and admin.py

parent 77168d34
...@@ -3,13 +3,13 @@ from .models import Author, Books ...@@ -3,13 +3,13 @@ from .models import Author, Books
# Register your models here. # Register your models here.
class AuthorAdmin(admin.ModelAdmin): class AuthorAdmin(admin.ModelAdmin):
model = Author model = Author
list_display = ('first_name', 'last_name', 'age', 'nationality', 'bio') list_display = ('first_name', 'last_name', 'age', 'nationality',)
search_fields = ['first_name', 'last_name',] search_fields = ['first_name', 'last_name',]
list_filter = ('first_name', 'last_name',) list_filter = ('first_name', 'last_name',)
class BooksAdmin(admin.ModelAdmin): class BooksAdmin(admin.ModelAdmin):
model = Books model = Books
list_display = ('title', 'author', 'publisher', 'year_published', 'ISBN', 'blurb') list_display = ('title', 'author', 'publisher', 'year_published', 'ISBN',)
search_fields = ['title', 'author', 'publisher', 'ISBN'] search_fields = ['title', 'author', 'publisher', 'ISBN']
list_filter = ('author', 'publisher', 'year_published',) list_filter = ('author', 'publisher', 'year_published',)
......
# Generated by Django 4.1.3 on 2023-03-27 07:23
import bookshelf.models
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('bookshelf', '0001_initial'),
]
operations = [
migrations.AlterField(
model_name='books',
name='blurb',
field=models.TextField(validators=[bookshelf.models.Books.validate_wordCount]),
),
migrations.AlterField(
model_name='books',
name='year_published',
field=models.IntegerField(blank=True, null=True, validators=[bookshelf.models.Books.validate_year]),
),
]
from datetime import datetime
from django.db import models from django.db import models
from django.core.exceptions import ValidationError from django.core.exceptions import ValidationError
from django.core.validators import MinLengthValidator
# Create your models here. # Create your models here.
class Author(models.Model): class Author(models.Model):
...@@ -17,16 +17,27 @@ class Books(models.Model): ...@@ -17,16 +17,27 @@ class Books(models.Model):
title = models.CharField(max_length = 100) title = models.CharField(max_length = 100)
author = models.ForeignKey(Author, on_delete = models.CASCADE) author = models.ForeignKey(Author, on_delete = models.CASCADE)
publisher = models.CharField(max_length = 100) publisher = models.CharField(max_length = 100)
year_published = models.DateTimeField('date published')
def validate_year(value):
if value > int(datetime.now().strftime("%Y")) or value < 1000:
raise ValidationError('Please enter a valid year (e.g. 2023)')
def validate_ISBN(value): def validate_ISBN(value):
if len(value) != 13: if len(value) != 13:
raise ValidationError('Must be exactly 13 digits.') raise ValidationError('Must be exactly 13 digits.')
if not value.isdigit(): if not value.isdigit():
raise ValidationError('Must only be digits.') raise ValidationError('Must only be digits.')
def validate_wordCount(value):
word_count = len(value.split())
if word_count < 100:
raise ValidationError('Word count is less than 100.')
if word_count > 200:
raise ValidationError('Word count is greater than 200.')
year_published = models.IntegerField(validators=[validate_year], blank=True, null=True)
ISBN = models.CharField(max_length=13, validators=[validate_ISBN]) ISBN = models.CharField(max_length=13, validators=[validate_ISBN])
blurb = models.TextField(max_length = 200, validators=[MinLengthValidator(100)]) blurb = models.TextField(validators=[validate_wordCount])
def __str__(self): def __str__(self):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment