Commit 77168d34 authored by Ysabella Panghulan's avatar Ysabella Panghulan

added bookshelf in project's installed apps, made migrations and registered models in admin

parent 8cf81673
...@@ -36,6 +36,7 @@ ALLOWED_HOSTS = [] ...@@ -36,6 +36,7 @@ ALLOWED_HOSTS = []
# Application definition # Application definition
INSTALLED_APPS = [ INSTALLED_APPS = [
'bookshelf.apps.BookshelfConfig',
'django.contrib.admin', 'django.contrib.admin',
'django.contrib.auth', 'django.contrib.auth',
'django.contrib.contenttypes', 'django.contrib.contenttypes',
......
from django.contrib import admin from django.contrib import admin
from .models import Author, Books
# Register your models here. # Register your models here.
class AuthorAdmin(admin.ModelAdmin):
model = Author
list_display = ('first_name', 'last_name', 'age', 'nationality', 'bio')
search_fields = ['first_name', 'last_name',]
list_filter = ('first_name', 'last_name',)
class BooksAdmin(admin.ModelAdmin):
model = Books
list_display = ('title', 'author', 'publisher', 'year_published', 'ISBN', 'blurb')
search_fields = ['title', 'author', 'publisher', 'ISBN']
list_filter = ('author', 'publisher', 'year_published',)
admin.site.register(Author, AuthorAdmin)
admin.site.register(Books, BooksAdmin)
\ No newline at end of file
# Generated by Django 4.1.3 on 2023-03-27 05:12
import bookshelf.models
import django.core.validators
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
initial = True
dependencies = [
]
operations = [
migrations.CreateModel(
name='Author',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('first_name', models.CharField(max_length=50)),
('last_name', models.CharField(max_length=50)),
('age', models.IntegerField(default=0)),
('nationality', models.CharField(max_length=50)),
('bio', models.TextField(max_length=700)),
],
),
migrations.CreateModel(
name='Books',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('title', models.CharField(max_length=100)),
('publisher', models.CharField(max_length=100)),
('year_published', models.DateTimeField(verbose_name='date published')),
('ISBN', models.CharField(max_length=13, validators=[bookshelf.models.Books.validate_ISBN])),
('blurb', models.TextField(max_length=200, validators=[django.core.validators.MinLengthValidator(100)])),
('author', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='bookshelf.author')),
],
),
]
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):
...@@ -14,17 +15,19 @@ class Author(models.Model): ...@@ -14,17 +15,19 @@ class Author(models.Model):
class Books(models.Model): 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') year_published = models.DateTimeField('date published')
ISBN = models.CharField(max_length=13, validators=[validate_ISBN])
blurb = models.TextField(max_length = 200, min_length = 100)
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.')
ISBN = models.CharField(max_length=13, validators=[validate_ISBN])
blurb = models.TextField(max_length = 200, validators=[MinLengthValidator(100)])
def __str__(self): def __str__(self):
return self.title return self.title
\ No newline at end of file
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