1

我正在使用一个名为“django-admin-sortable2”的包,但我不明白我在编码什么。有人可以解释一下吗?

这是我使用的:https ://django-admin-sortable2.readthedocs.io/en/latest/usage.html#sortable-many-to-many-relations-with-sortable-tabular-inlines

这是 GitHub 存储库:https ://github.com/jrief/django-admin-sortable2

这是他们使用的代码示例:

模型.py

from django.db.import models

class Button(models.Model):
    """A button"""
    name = models.CharField(max_length=64)
    button_text = models.CharField(max_length=64)

class Panel(models.Model):
    """A Panel of Buttons - this represents a control panel."""
    name = models.CharField(max_length=64)
    buttons = models.ManyToManyField(Button, through='PanelButtons')

class PanelButtons(models.Model):
    """This is a junction table model that also stores the button order for a panel."""
    panel = models.ForeignKey(Panel)
    button = models.ForeignKey(Button)
    button_order = models.PositiveIntegerField(default=0)

    class Meta:
        ordering = ('button_order',)

管理员.py

from django.contrib import admin
from adminsortable2.admin import SortableInlineAdminMixin
from models import Panel

class ButtonTabularInline(SortableInlineAdminMixin, admin.TabularInline):
    # We don't use the Button model but rather the juction model specified on Panel.
    model = Panel.buttons.through

@admin.register(Panel)
class PanelAdmin(admin.ModelAdmin)
    inlines = (ButtonTabularInline,)
4

0 回答 0