Commit a5d1ce08 authored by Deokhyun Lee's avatar Deokhyun Lee

renamed the model books to book (name of the models should be singular)

parent 587a7933
from django.contrib import admin
from .models import Author, Books
from .models import Author, Book
# admin panel for Author model
......@@ -13,12 +13,12 @@ class AuthorAdmin(admin.ModelAdmin):
# admin panel for Books model
class BooksAdmin(admin.ModelAdmin):
model = Books
model = Book
list_display = ('title', 'author', 'publisher', 'year_published', 'ISBN', 'blurb')
search_fields = ('title', 'author', 'publisher', 'year_published', 'ISBN', 'blurb')
list_filter = ('title', 'author', 'publisher', 'year_published', 'ISBN', 'blurb')
admin.site.register(Author, AuthorAdmin)
admin.site.register(Books, BooksAdmin)
admin.site.register(Book, BooksAdmin)
from django import forms
from .models import Books
from .models import Book, Author
class AddBookForm(forms.ModelForm):
class Meta:
model = Books
model = Book
fields = '__all__'
class AddAuthorForm(forms.ModelForm):
class Meta:
model = Author
fields = '__all__'
# def save(self, commit=True):
# book = super().save(commit=False)
# # Do any additional processing of the form data here
# if commit:
# book.save()
# return book
# Generated by Django 4.1.7 on 2023-04-25 11:22
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('bookshelf', '0001_initial'),
]
operations = [
migrations.CreateModel(
name='Book',
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='year published')),
('ISBN', models.CharField(max_length=13)),
('blurb', models.CharField(max_length=250)),
('author', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='bookshelf.author')),
],
),
migrations.DeleteModel(
name='Books',
),
]
# Generated by Django 4.1.7 on 2023-04-25 11:24
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('bookshelf', '0002_book_delete_books'),
]
operations = [
migrations.RenameModel(
old_name='Book',
new_name='Books',
),
]
# Generated by Django 4.1.7 on 2023-04-25 11:26
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('bookshelf', '0003_rename_book_books'),
]
operations = [
migrations.RenameModel(
old_name='Books',
new_name='Book',
),
]
......@@ -14,12 +14,12 @@ class Author(models.Model):
return self.first_name + " " + self.last_name
# Books model
class Books(models.Model):
# Book model
class Book(models.Model):
title = models.CharField(max_length = 100)
author = models.ForeignKey(Author, on_delete = models.CASCADE)
publisher = models.CharField(max_length = 100)
year_published = models.DateTimeField("date published")
year_published = models.DateTimeField("year published")
ISBN = models.CharField(max_length = 13)
blurb = models.CharField(max_length = 250)
......
......@@ -2,7 +2,7 @@ from django.views import View
from django.shortcuts import render, redirect
from django.views.generic import ListView, DetailView, CreateView
from django.shortcuts import render
from .models import Author, Books
from .models import Author, Book
from .forms import AddBookForm
# View for Home (landing page)
......@@ -27,23 +27,23 @@ class AuthorDetailView(DetailView):
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
# get by self == Author's id to get Book's foreign key that matches up.
context['books'] = Books.objects.filter(author = self.object)
context['books'] = Book.objects.filter(author = self.object)
return context
# Views for Books page
class BookListView(ListView):
model = Books
model = Book
template_name = 'books/books.html'
context_object_name = 'books'
ordering = ['id']
class BookDetailView(DetailView):
model = Books
model = Book
template_name = 'books/book_details.html'
context_object_name = 'book'
class BookAddListView(CreateView):
model = Books
model = Book
fields = '__all__'
template_name = "books/add-book.html"
......@@ -55,4 +55,5 @@ class BookAddListView(CreateView):
# pass the pk and redirect.
return redirect('book_detail', pk = new_book.pk)
else:
return render(request, 'book_details.html', {'form': form})
\ No newline at end of file
return render(request, 'book_details.html', {'form': form})
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