Commit a3af8e9b authored by impfundev's avatar impfundev

feat: book relation to category

parent c2f59edd
...@@ -5,7 +5,7 @@ from books.models import Book ...@@ -5,7 +5,7 @@ from books.models import Book
class BookForm(forms.ModelForm): class BookForm(forms.ModelForm):
class Meta: class Meta:
model = Book model = Book
fields = ["title", "stock", "description"] fields = ["title", "stock", "category", "description"]
widgets = { widgets = {
"title": forms.TextInput( "title": forms.TextInput(
...@@ -21,6 +21,11 @@ class BookForm(forms.ModelForm): ...@@ -21,6 +21,11 @@ class BookForm(forms.ModelForm):
"class": "form-control", "class": "form-control",
} }
), ),
"category": forms.Select(
attrs={
"class": "form-control",
}
),
"description": forms.Textarea( "description": forms.Textarea(
attrs={ attrs={
"placeholder": "Description", "placeholder": "Description",
......
# Generated by Django 5.0.6 on 2024-07-09 12:03
import django.db.models.deletion
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('books', '0002_book_stock'),
('categories', '0001_initial'),
]
operations = [
migrations.AddField(
model_name='book',
name='category',
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, to='categories.category'),
),
]
from django.db import models from django.db import models
from categories.models import Category
class Book(models.Model): class Book(models.Model):
title = models.CharField(max_length=100) title = models.CharField(max_length=100)
description = models.CharField(max_length=255) description = models.CharField(max_length=255)
stock = models.BigIntegerField(blank=True, null=True) stock = models.BigIntegerField(blank=True, null=True)
category = models.ForeignKey(
to=Category, on_delete=models.CASCADE, blank=True, null=True
)
created_at = models.DateTimeField(auto_now_add=True) created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True) updated_at = models.DateTimeField(auto_now=True)
......
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
<thead> <thead>
<tr class="table-primary"> <tr class="table-primary">
<th scope="col">Title</th> <th scope="col">Title</th>
<th scope="col">Category</th>
<th scope="col">Stock</th> <th scope="col">Stock</th>
<th scope="col">Description</th> <th scope="col">Description</th>
<th scope="col">Created At</th> <th scope="col">Created At</th>
...@@ -13,6 +14,7 @@ ...@@ -13,6 +14,7 @@
{% if object_list %} {% for book in object_list %} {% if object_list %} {% for book in object_list %}
<tr> <tr>
<td>{{ book.title }}</td> <td>{{ book.title }}</td>
<td>{{ book.category.name }}</td>
<td>{{ book.stock }}</td> <td>{{ book.stock }}</td>
<td>{{ book.description }}</td> <td>{{ book.description }}</td>
<td>{{ book.created_at }}</td> <td>{{ book.created_at }}</td>
......
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