diff --git a/backend/book_api/admin.py b/backend/book_api/admin.py index 503dec2..3b117b0 100644 --- a/backend/book_api/admin.py +++ b/backend/book_api/admin.py @@ -1,8 +1,30 @@ from django.contrib import admin +from book_api.models import Genre +from book_api.models import Topic from book_api.models import Book +@admin.register(Genre) +class GenreAdmin(admin.ModelAdmin): + list_display = [ + 'id', 'name', + ] + list_display_links = ['name'] + search_fields = ['name'] + readonly_fields = ['id'] + + +@admin.register(Topic) +class TopicAdmin(admin.ModelAdmin): + list_display = [ + 'id', 'name', + ] + list_display_links = ['name'] + search_fields = ['name'] + readonly_fields = ['id'] + + @admin.register(Book) class BookAdmin(admin.ModelAdmin): list_display = [ @@ -14,7 +36,7 @@ class BookAdmin(admin.ModelAdmin): 'title', ] list_filter = [ - 'authors', 'publisher', + 'genres', 'topics', 'authors', 'publisher', ] search_fields = [ 'title', 'edition', 'isbn', diff --git a/backend/book_api/migrations/0005_genre_book_genres.py b/backend/book_api/migrations/0005_genre_book_genres.py new file mode 100644 index 0000000..9047285 --- /dev/null +++ b/backend/book_api/migrations/0005_genre_book_genres.py @@ -0,0 +1,25 @@ +# Generated by Django 5.1.1 on 2024-09-06 21:17 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('book_api', '0004_alter_book_book'), + ] + + operations = [ + migrations.CreateModel( + name='Genre', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('name', models.CharField(max_length=255, unique=True)), + ], + ), + migrations.AddField( + model_name='book', + name='genres', + field=models.ManyToManyField(related_name='books', to='book_api.genre'), + ), + ] diff --git a/backend/book_api/migrations/0006_topic.py b/backend/book_api/migrations/0006_topic.py new file mode 100644 index 0000000..4953376 --- /dev/null +++ b/backend/book_api/migrations/0006_topic.py @@ -0,0 +1,20 @@ +# Generated by Django 5.1.1 on 2024-09-06 21:21 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('book_api', '0005_genre_book_genres'), + ] + + operations = [ + migrations.CreateModel( + name='Topic', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('name', models.CharField(max_length=255, unique=True)), + ], + ), + ] diff --git a/backend/book_api/migrations/0007_book_topics.py b/backend/book_api/migrations/0007_book_topics.py new file mode 100644 index 0000000..e59c88b --- /dev/null +++ b/backend/book_api/migrations/0007_book_topics.py @@ -0,0 +1,18 @@ +# Generated by Django 5.1.1 on 2024-09-06 21:23 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('book_api', '0006_topic'), + ] + + operations = [ + migrations.AddField( + model_name='book', + name='topics', + field=models.ManyToManyField(related_name='books', to='book_api.topic'), + ), + ] diff --git a/backend/book_api/models.py b/backend/book_api/models.py index 017ee3e..26b28f0 100644 --- a/backend/book_api/models.py +++ b/backend/book_api/models.py @@ -17,8 +17,30 @@ def book_upload_path(instance, filename): return os.path.join('books', new_filename) +class Genre(models.Model): + name = models.CharField(max_length=255, unique=True) + + def __str__(self) -> str: + return self.name + + +class Topic(models.Model): + name = models.CharField(max_length=255, unique=True) + + def __str__(self) -> str: + return self.name + + class Book(models.Model): title = models.CharField(max_length=255) + genres = models.ManyToManyField( + 'book_api.Genre', + related_name='books' + ) + topics = models.ManyToManyField( + 'book_api.Topic', + related_name='books' + ) authors = models.ManyToManyField( 'author_api.Author', related_name='books') diff --git a/backend/db.sqlite3 b/backend/db.sqlite3 index a3319bb..6a5f165 100644 Binary files a/backend/db.sqlite3 and b/backend/db.sqlite3 differ