Add models and registered in admin

parent 6f914ae2
from django.contrib import admin
# Register your models here.
from .models import Author, Books
class AuthorAdmin(admin.ModelAdmin):
model = Author
list_display = ('first_name', 'last_name', 'age', 'nationality',)
class BooksAdmin(admin.ModelAdmin):
model = Books
list_display = ('title', 'author', 'publisher', 'year_published', 'isbn',)
admin.site.register(Author, AuthorAdmin)
admin.site.register(Books, BooksAdmin)
\ No newline at end of file
# Generated by Django 4.1.6 on 2023-03-28 11:01
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(default='', max_length=200)),
('last_name', models.CharField(default='', max_length=200)),
('age', models.IntegerField(default=0)),
('nationality', models.CharField(default='', max_length=200)),
('bio', models.TextField(default='', 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(default='', max_length=200)),
('publisher', models.CharField(default='', max_length=200)),
('year_published', models.IntegerField(default=2023, validators=[django.core.validators.MaxValueValidator(2023)])),
('isbn', models.IntegerField(default=0, validators=[django.core.validators.MinValueValidator(1), django.core.validators.MaxValueValidator(9999999999999)])),
('blurb', models.TextField(default='', max_length=200)),
('author', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, to='bookshelf.author')),
],
),
]
from django.db import models
from django.core.validators import MinValueValidator, MaxValueValidator
import datetime
# Create your models here.
class Author(models.Model):
default_string = ""
first_name = models.CharField(max_length = 200, default = default_string)
last_name = models.CharField(max_length = 200, default = default_string)
age = models.IntegerField(default = 0)
nationality = models.CharField(max_length = 200, default = default_string)
bio = models.TextField(max_length = 700, default = default_string)
def __str__(self):
return '{} {}'.format(self.first_name, self.last_name)
class Books(models.Model):
default_string = ""
title = models.CharField(max_length = 200, default = default_string)
author = models.ForeignKey(Author, on_delete = models.PROTECT)
publisher = models.CharField(max_length = 200, default = default_string)
year_published = models.IntegerField(validators = [MaxValueValidator(datetime.datetime.now().year)], default = datetime.datetime.now().year)
isbn = models.IntegerField(validators = [MinValueValidator(1), MaxValueValidator(9999999999999)], default = 0)
blurb = models.TextField(max_length = 200, default = default_string)
def __str__(self):
return '{}'.format(self.title)
from django.urls import path
from .views import index
urlpatterns = [
path('', index, name='index'),
]
app_name = "bookshelf"
\ No newline at end of file
from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
def index(request):
return HttpResponse("Welcome to Gareth's Music Library!")
\ No newline at end of file
......@@ -14,8 +14,9 @@ Including another URLconf
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path
from django.urls import include, path
urlpatterns = [
path('admin/', admin.site.urls),
path('Bookshelf/', include('bookshelf.urls', namespace = 'Bookshelf'))
]
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