Commit 890a7d8a authored by Javi Ng's avatar Javi Ng

wrote code for models and admin pages

parent ce941a5a
No preview for this file type
from django.contrib import admin from django.contrib import admin
from .models import Author, Book
# Register your models here. # Register your models here.
# show books in page for authors
class BookInline(admin.TabularInline):
model = Book
class AuthorAdmin(admin.ModelAdmin):
model = Author
list_display = ("first_name", "last_name", "age", "nationality")
search_fields = ("first_name", "last_name")
list_filter = ("nationality")
inlines = [BookInline]
class BookAdmin(admin.ModelAdmin):
list_display = ("title", "get_author", "year_published", "ISBN")
list_filter = ("get_author", "publisher")
def get_author(self, obj):
return obj.author.title
# title of column for get_author
get_author.short_description = "Author"
# sorting by Author
get_author.admin_order_field = "Author"
admin.site.register(Author, AuthorAdmin)
admin.site.register(Book, BookAdmin)
\ No newline at end of file
from django.db import models from django.db import models
# Create your models here. # Create your models here.
class Author (models.Model):
first_name = models.CharField(max_length = 50)
last_name = models.CharField(max_length = 30)
age = models.IntegerField(default = 10)
nationality = models.CharField(max_length = 50)
bio = models.TextField(max_length = 700)
class Book (models.Model):
title = models.CharField(max_length = 50)
author = models.ForeignKey(Author, on_delete = models.CASCADE)
publisher = models.CharField(max_length = 100)
year_published = models.IntegerField(default = 1984)
ISBN = models.IntegerField(
max_value = 9999999999999,
min_value = 1000000000000
)
blurb = models.TextField(min_length = 100, max_length = 200)
\ 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