Add section on many-to-many relations

master
blag 2020-07-22 01:51:22 -07:00
parent 7ed44fa84b
commit 5e9e0b3564
No known key found for this signature in database
GPG Key ID: 30870D32F59C5F40
1 changed files with 54 additions and 0 deletions

View File

@ -200,6 +200,60 @@ admin.site.register(Category, CategoryAdmin)
The `NonSortableParentAdmin` class is necessary to wire up the additional URL patterns and JavaScript that Django Admin Sortable needs to make your models sortable. The child model does not have to be an inline model, it can be wired directly to Django admin and the objects will be grouped by the non-sortable foreign key when sorting.
#### Sortable Many-to-Many Model
It is also possible to make many-to-many relations sortable, but it requires an explicit many-to-many model.
`models.py`:
```python
from django.db import models
from adminsortable.models import SortableMixin
from adminsortable.fields import SortableForeignKey
class Image(models.Model):
...
class Gallery(models.Model):
class Meta:
verbose_name_plural = 'Galleries'
...
images = models.ManyToManyField(
Image,
through_fields=('gallery', 'image'),
through='GalleryImageRelation',
verbose_name=_('Images')
)
class GalleryImageRelation(SortableMixin):
"""Many to many relation that allows users to sort images in galleries"""
class Meta:
ordering = ['image_order']
gallery = models.ForeignKey(Gallery, verbose_name=_("Gallery"))
image = SortableForeignKey(Image, verbose_name=_("Image"))
image_order = models.PositiveIntegerField(default=0, editable=False, db_index=True)
```
`admin.py`:
```python
from django.contrib import admin
from adminsortable.admin import (SortableAdmin, SortableTabularInline)
from .models import (Image, Gallery, GalleryImageRelation)
class GalleryImageRelationInlineAdmin(SortableTabularInline):
model = GalleryImageRelation
extra = 1
class GalleryAdmin(NonSortableParentAdmin):
inlines = (GalleryImageRelationInlineAdmin,)
admin.site.register(Image, admin.ModelAdmin)
admin.site.register(Gallery, GalleryAdmin)
```
Any non-editable space in each rendered inline will let you drag and drop them into order.
### Backwards Compatibility
If you previously used Django Admin Sortable, **DON'T PANIC** - everything will still work exactly as before ***without any changes to your code***. Going forward, it is recommended that you use the new `SortableMixin` on your models, as pre-2.0 compatibility might not be a permanent thing.