Is there a reason the one-to-many case isn't documented?
Opened this issue · 1 comments
pandichef commented
I'm trying to a one-to-many (e.g., one Article instance can only be part of one ArticleSeries). Can someone outline how I can do this? The docs skips over this case and go straight to many-to-many.
joshuanianji commented
One-to-many relations are technically documented, but the title is pretty implicit (see: "In Django’s Admin Detail View, make Stacked- and Tabular-Inlines sortable"), and the code isn't very complete. Here's a fuller example of their "SortableChapter" and "Book" models:
# models.py
from django.db import models
class Book(models.Model):
# if we want to sort Books as well, add an ordering field and Meta class
title = models.CharField("Title", max_length=255)
class SortableChapter(models.Model):
title = models.CharField("Title", max_length=255)
book = models.ForeignKey(Book, on_delete=models.CASCADE)
order = models.PositiveIntegerField(
default=0,
blank=False,
null=False,
)
class Meta:
ordering = ['order']
# models.py
from django.contrib import admin
from adminsortable2.admin import SortableStackedInline, SortableAdminBase
from . import models
class ChapterStackedInline(SortableStackedInline):
model = models.SortableChapter
extra = 1
@admin.register(models.Book)
# if you to sort Books as well, use `SortableAdminMixin`
class BookAdmin(SortableAdminBase, admin.ModelAdmin):
inlines = [ChapterStackedInline]
The resulting admin dashboard looks like this (don't mind the chapter names):