[Answered ]-How to select multiple relative objects of a single object at once Django ForeignKey

1๐Ÿ‘

โœ…

If you wanna edit multiple books from a category that belongs to it, you can keep the first piece of code and add Inlines to category from admin.py

models.py

class Category(models.Model):
    class Meta:
        verbose_name_plural = "Categories"

    category = models.CharField(max_length=20)

    def __str__(self):
        return self.category

class Book(models.Model):
    book_title = models.CharField(max_length=20)
    category = models.ForeignKey(Category, on_delete=models.CASCADE)
    def __str__(self):
        return self.book_title

admin.py

from django.contrib import admin
from .models import *

@admin.register(Book)
class BookAdmin(admin.ModelAdmin):
    pass


class BookInline(admin.TabularInline):
    model = Book



@admin.register(Category)
class CategoryAdmin(admin.ModelAdmin):
    inlines = [BookInline]

๐Ÿ‘คKrish agrawal

Leave a comment