Skip to main content

Drop in replacement for django's many to many field with sorted relations.

Project description

sortedm2m is a drop in replacement for django’s own ManyToManyField. The provided SortedManyToManyField behaves like the original one but remembers the order of added relations.

sortedm2m requires at least django 1.2. Django 1.1 or earlier is not supported.

Usecases

Imagine that you have a gallery model and a photo model. Usually you want a relation between these models so you can add multiple photos to one gallery but also want to be able to have the same photo on many galleries.

This is where you usually can use many to many relation. The downside is that django’s default implementation doesn’t provide a way to order the photos in the gallery. So you only have a random ordering which is not suitable in most cases.

You can work around this limitation by using the SortedManyToManyField provided by this package as drop in replacement for django’s ManyToManyField.

Usage

Use SortedManyToManyField like ManyToManyField in your models:

from django.db import models
from sortedm2m.fields import SortedManyToManyField

class Photo(models.Model):
    name = models.CharField(max_length=50)
    image = models.ImageField(upload_to='...')

class Gallery(models.Model):
    name = models.CharField(max_length=50)
    photos = SortedManyToManyField(Photo)

If you use the relation in your code like the following, it will remember the order in which you have added photos to the gallery.

gallery = Gallery.objects.create(name='Photos ordered by name')
for photo in Photo.objects.order_by('name'):
    gallery.photos.add(photo)

Admin

SortedManyToManyField provides a custom widget which can be used to sort the selected items. It renders a list of checkboxes that can be sorted by drag’n’drop.

It’s also possible to use the SortedManyToManyField with admin’s raw_id_fields option in the ModelAdmin definition. Add the name of the SortedManyToManyField to this list to get a simple text input field. The order in which the ids are entered into the input box is used to sort the items of the sorted m2m relation.

Example:

from django.contrib import admin

class GalleryAdmin(admin.ModelAdmin):
    raw_id_fields = ('photos',)

Contribue

You can find the latest development version on github. Get there and fork it, file bugs or send me nice wishes.

Feel free to drop me a message about critique or feature requests. You can get in touch with me by mail or twitter.

Authors

Changelog

0.3.1

  • Fixed packaging error.

0.3.0

  • Heavy internal refactorings. These were necessary to solve a problem with SortedManyToManyField and a reference to 'self'.

0.2.5

  • Forgot to exclude debug print/console.log statements from code. Sorry.

0.2.4

  • Fixing problems with SortedCheckboxSelectMultiple widget, especially in admin where a “create and add another item” popup is available.

0.2.3

  • Fixing issue with primary keys instead of model instances for .add() and .remove() methods in SortedRelatedManager.

0.2.2

  • Fixing validation error for SortedCheckboxSelectMultiple. It caused errors if only one value was passed.

0.2.1

  • Removed unnecessary reference of jquery ui css file in SortedCheckboxSelectMultiple. Thanks to Klaas van Schelven and Yuwei Yu for the hint.

0.2.0

  • Added a widget for use in admin.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-sortedm2m-0.3.1.tar.gz (25.1 kB view details)

Uploaded Source

File details

Details for the file django-sortedm2m-0.3.1.tar.gz.

File metadata

File hashes

Hashes for django-sortedm2m-0.3.1.tar.gz
Algorithm Hash digest
SHA256 7b09a003d4c8abfb56a6db89001c66553b1588c953a36334eba0072cd0d8fd51
MD5 26a369fcd5514978edeb90fce3449a09
BLAKE2b-256 bdbf36ae3fafe239f56eae831274bbafc0e07dcd6c97ead0713dd8e80c447330

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page