Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Is there a reason the one-to-many case isn't documented? #342

Open
pandichef opened this issue Jan 16, 2023 · 1 comment
Open

Is there a reason the one-to-many case isn't documented? #342

pandichef opened this issue Jan 16, 2023 · 1 comment

Comments

@pandichef
Copy link

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
Copy link

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):

Screenshot 2023-07-14 at 11 31 01 AM

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants