I am newbie in django,working on rest API framework. I want to ask the user for certain details to be put in html format, like popularity, name of some book.
A book can belong to various category such as Friction, Romantic, Action.
So I want to have a list Field for category.
Two options can be given, one ask the user to enter the category and append in the list at the backend. Or provide a list from which user can choose.
Have tried the below code in model.py
from django.db import models
category = models.CharField(max_length=100)
name = models.CharField(max_length=100)
date = models.DateTimeField()
category = models.ManyToManyField(Category)
#category = models.ForeignKeyField(Category) #tried Foreign key Model Field also
"name": "Harry Potter",
Each book belongs to only one category (or genre), so what you need is a foreign key (not many to many relationship). Your
Book model should look like below:
class Book(models.Model): title = models.CharField(max_length=100) publication_date = models.DateTimeField() category = models.ForeignKey(Category, on_delete=models.CASCADE) def __str__(self): return self.title
dateto not shadow
datein case you imported it from
datetimemodule for example.
on_delete=models.CASCADEmeans if a category is deleted, all its related books will be deleted too.