GithubHelp home page GithubHelp logo

isabella232 / django-sortedm2m Goto Github PK

View Code? Open in Web Editor NEW

This project forked from jazzband/django-sortedm2m

0.0 0.0 0.0 402 KB

A transparent sorted ManyToMany field for django.

Home Page: http://pypi.python.org/pypi/django-sortedm2m

License: BSD 3-Clause "New" or "Revised" License

Makefile 0.60% Python 86.53% HTML 2.16% CSS 1.56% JavaScript 9.15%

django-sortedm2m's Introduction

django-sortedm2m

Jazzband PyPI Release Build Status Code coverage

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.

Use Cases

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.

Requirements

django-sortedm2m runs on Python 2.7, 3.5+ and Django 1.11 to 2.2.

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)

SortedManyToManyField

You can use the following arguments to modify the default behavior:

sorted

Default: True

You can set the sorted to False which will force the SortedManyToManyField in behaving like Django's original ManyToManyField. No ordering will be performed on relation nor will the intermediate table have a database field for storing ordering information.

sort_value_field_name

Default: 'sort_value'

Specifies how the field is called in the intermediate database table by which the relationship is ordered. You can change its name if you have a legacy database that you need to integrate into your application.

base_class

Default: None

You can set the base_class, which is the base class of the through model of the sortedm2m relationship between models to an abstract base class containing a __str__ method to improve the string representations of sortedm2m relationships.

Note

You also could use it to add additional fields to the through model. But please beware: These fields will not be created or modified by an automatically created migration. You will need to take care of migrations yourself. In most cases when you want to add another field, consider not using sortedm2m but use a ordinary Django ManyToManyField and specify your own through model.

Migrating a ManyToManyField to be a SortedManyToManyField

If you are using Django's migration framework and want to change a ManyToManyField to be a SortedManyToManyField (or the other way around), you will find that a migration created by Django's makemigrations will not work as expected.

In order to migrate a ManyToManyField to a SortedManyToManyField, you change the field in your models to be a SortedManyToManyField as appropriate and create a new migration with manage.py makemigrations. Before applying it, edit the migration file and change in the operations list migrations.AlterField to AlterSortedManyToManyField (import it from sortedm2m.operations). This operation will take care of changing the intermediate tables, add the ordering field and fill in default values.

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.

To use the widget in the admin you need to add sortedm2m to your INSTALLED_APPS settings, like:

INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.contrib.admin',

    'sortedm2m',

    '...',
)

Otherwise it will not find the css and js files needed to sort by drag'n'drop.

Finally, make sure not to have the model listed in any filter_horizontal or filter_vertical tuples inside of your ModelAdmin definitions.

If you did it right, you'll wind up with something like this:

http://i.imgur.com/HjIW7MI.jpg

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',)

Contribute

This is a Jazzband project. By contributing you agree to abide by the Contributor Code of Conduct and follow the guidelines.

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

Running the tests

I recommend to use tox to run the tests for all relevant python versions all at once. Therefore install tox with pip install tox, then type in the root directory of the django-sortedm2m checkout:

tox

The tests are run against SQLite, then against PostgreSQL, then against mySQL - so you need to install PostgreSQL and mySQL on your dev environment, and should have a role/user sortedm2m set up for both PostgreSQL and mySQL.

Code Quality

This project uses isort, pycodestyle, and pylint to manage validate code quality. These validations can be run with the following command:

tox -e quality

django-sortedm2m's People

Contributors

akaihola avatar appden avatar benthompson avatar cchurch avatar clintonb avatar cuchac avatar dragoonaethis avatar erm0l0v avatar fcurella avatar gorandev avatar gregmuellegger avatar hstanev avatar jezdez avatar jlemaes avatar johnraz avatar jonny5532 avatar mikeknoop avatar mitchellrj avatar mystic-mirage avatar nemesifier avatar ninjadero avatar outime avatar quantum5 avatar richardbarran avatar rohithasrk avatar rolandgeider avatar seanoc avatar smcoll avatar tipuch avatar vxsx avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.