Commit a3af8e9b authored by impfundev's avatar impfundev

feat: book relation to category

parent c2f59edd
......@@ -5,7 +5,7 @@ from books.models import Book
class BookForm(forms.ModelForm):
class Meta:
model = Book
fields = ["title", "stock", "description"]
fields = ["title", "stock", "category", "description"]
widgets = {
"title": forms.TextInput(
......@@ -21,6 +21,11 @@ class BookForm(forms.ModelForm):
"class": "form-control",
}
),
"category": forms.Select(
attrs={
"class": "form-control",
}
),
"description": forms.Textarea(
attrs={
"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 categories.models import Category
class Book(models.Model):
title = models.CharField(max_length=100)
description = models.CharField(max_length=255)
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)
updated_at = models.DateTimeField(auto_now=True)
......
......@@ -2,6 +2,7 @@
<thead>
<tr class="table-primary">
<th scope="col">Title</th>
<th scope="col">Category</th>
<th scope="col">Stock</th>
<th scope="col">Description</th>
<th scope="col">Created At</th>
......@@ -13,6 +14,7 @@
{% if object_list %} {% for book in object_list %}
<tr>
<td>{{ book.title }}</td>
<td>{{ book.category.name }}</td>
<td>{{ book.stock }}</td>
<td>{{ book.description }}</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