I am using a package called "django-admin-sortable2" but I do not understand what I am coding. May someone explain?
Here's what I used: https://django-admin-sortable2.readthedocs.io/en/latest/usage.html#sortable-many-to-many-relations-with-sortable-tabular-inlines
Here's the GitHub repository: https://github.com/jrief/django-admin-sortable2
Here's the code example they used:
models.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',)
admin.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,)