updated models and admin

parent 4cad9946
from django.contrib import admin
from .models import Author, Books
# Register your models here.
class AuthorAdmin(admin.ModelAdmin):
model = Author
list_display = ('first_name', 'last_name', 'age', 'nationality', 'bio')
class BooksAdmin(admin.ModelAdmin):
model = Books
list_display = ('title', 'author', 'publisher', 'year_published', 'ISBN', 'blurb')
# registering the model and the admin is what tells
# Django that admin pages must be generated for the models specified
admin.site.register(Author, AuthorAdmin)
admin.site.register(Books, BooksAdmin)
\ No newline at end of file
# Generated by Django 4.1.7 on 2023-03-28 10:32
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='')),
('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.TextField(default='', max_length=200)),
('publisher', models.TextField(default='', max_length=200)),
('year_published', models.IntegerField(default=2023, max_length=2023)),
('ISBN', models.PositiveBigIntegerField(validators=[django.core.validators.MinValueValidator(1000000000000), django.core.validators.MaxValueValidator(9999999999999)])),
('blurb', models.TextField(max_length=200, validators=[django.core.validators.MinLengthValidator(100)])),
('author', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, to='bookshelf.author')),
],
),
]
# Generated by Django 4.1.7 on 2023-03-28 10:33
import django.core.validators
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('bookshelf', '0001_initial'),
]
operations = [
migrations.AlterField(
model_name='books',
name='year_published',
field=models.IntegerField(default=2023, validators=[django.core.validators.MaxValueValidator(2023)]),
),
]
from django.db import models
from django.urls import reverse
from django.core.validators import MinLengthValidator,MinValueValidator,MaxValueValidator
import datetime
class Author(models.Model):
......@@ -11,10 +12,10 @@ class Author(models.Model):
def __str__(self):
return '{}, {} mode'.format(self.first_name, self.last_name)
return '{} {}'.format(self.first_name, self.last_name)
# def get_absolute_url(self):
# return reverse('location_detail', args=[str(self.first_name)])
def get_absolute_url(self):
return reverse('location_detail', args=[str(self.first_name)])
class Books(models.Model):
......@@ -25,15 +26,16 @@ class Books(models.Model):
)
publisher = models.TextField(max_length=200, default='')
#Source:https://stackoverflow.com/questions/1517474/only-showing-year-in-django-admin-a-yearfield-instead-of-datefield
year_published = models.IntegerField(max_length= datetime.datetime.now().year, default = datetime.datetime.now().year)
year_published = models.IntegerField(validators=[MaxValueValidator(datetime.datetime.now().year)], default = datetime.datetime.now().year)
ISBN = models.IntegerField(min_length = 1000000000000, max_length = 9999999999999)
blurb = models.TextField(min_length = 100, max_length=200)
#Source: https://stackoverflow.com/questions/30849862/django-max-length-for-integerfield
ISBN = models.PositiveBigIntegerField(validators=[MinValueValidator(1000000000000), MaxValueValidator(9999999999999)])
blurb = models.TextField(validators=[MinLengthValidator(100)], max_length=200)
def __str__(self):
return '{}, {} mode'.format(self.title, self.author)
# def get_absolute_url(self):
# return reverse('location_detail', args=[str(self.title)])
def get_absolute_url(self):
return reverse('location_detail', args=[str(self.title)])
from django.urls import path
from .views import index
from .views import index
urlpatterns = [
path('', index, name='index'),
path('', index, name='index'),
]
app_name = "bookshelf"
\ No newline at end of file
from django.http import HttpResponse
from django.shortcuts import render
from .models import Author, Books
# Create your views here.
def index(request):
return HttpResponse('Hello World! This came from the index view')
\ No newline at end of file
......@@ -17,6 +17,6 @@ from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('bookshelf/', include('bookshelf.urls', namespace='bookshelf')),
path('admin/', admin.site.urls),
]
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