Debian Codegraph @

PKG-INFO

raw
1 Metadata-Version: 2.1
2 Name: django-sortedm2m
3 Version: 3.1.1
4 Summary: Drop-in replacement for Django's many to many field with sorted relations.
5 Home-page: https://github.com/jazzband/django-sortedm2m
6 Author: Gregor Müllegger
7 Author-email: [email protected]
8 License: BSD
9 Platform: UNKNOWN
10 Classifier: Development Status :: 5 - Production/Stable
11 Classifier: Environment :: Web Environment
12 Classifier: Framework :: Django
13 Classifier: Framework :: Django :: 2.2
14 Classifier: Framework :: Django :: 3.0
15 Classifier: Framework :: Django :: 3.1
16 Classifier: Framework :: Django :: 3.2
17 Classifier: Intended Audience :: Developers
18 Classifier: License :: OSI Approved :: BSD License
19 Classifier: Natural Language :: English
20 Classifier: Operating System :: OS Independent
21 Classifier: Programming Language :: Python
22 Classifier: Programming Language :: Python :: 3.6
23 Classifier: Programming Language :: Python :: 3.7
24 Classifier: Programming Language :: Python :: 3.8
25 Classifier: Programming Language :: Python :: 3.9
26 License-File: LICENSE.txt
27 License-File: AUTHORS.rst
28
29 ================
30 django-sortedm2m
31 ================
32
33 .. image:: https://jazzband.co/static/img/badge.svg
34 :target: https://jazzband.co/
35 :alt: Jazzband
36
37 .. image:: https://img.shields.io/pypi/v/django-sortedm2m.svg
38 :target: https://pypi.python.org/pypi/django-sortedm2m
39 :alt: PyPI Release
40
41 .. image:: https://travis-ci.org/jazzband/django-sortedm2m.svg?branch=master
42 :target: https://travis-ci.org/jazzband/django-sortedm2m
43 :alt: Build Status
44
45 .. image:: https://codecov.io/gh/jazzband/django-sortedm2m/branch/master/graph/badge.svg
46 :target: https://codecov.io/gh/jazzband/django-sortedm2m
47 :alt: Code coverage
48
49 ``sortedm2m`` is a drop-in replacement for django's own ``ManyToManyField``.
50 The provided ``SortedManyToManyField`` behaves like the original one but
51 remembers the order of added relations.
52
53 Use Cases
54 =========
55
56 Imagine that you have a gallery model and a photo model. Usually you want a
57 relation between these models so you can add multiple photos to one gallery
58 but also want to be able to have the same photo on many galleries.
59
60 This is where you usually can use many to many relation. The downside is that
61 django's default implementation doesn't provide a way to order the photos in
62 the gallery. So you only have a random ordering which is not suitable in most
63 cases.
64
65 You can work around this limitation by using the ``SortedManyToManyField``
66 provided by this package as drop in replacement for django's
67 ``ManyToManyField``.
68
69 Requirements
70 ============
71
72 **django-sortedm2m** runs on Python 3.6+ and Django from 2.1 to 3.0.
73
74 Usage
75 =====
76
77 Use ``SortedManyToManyField`` like ``ManyToManyField`` in your models::
78
79 from django.db import models
80 from sortedm2m.fields import SortedManyToManyField
81
82 class Photo(models.Model):
83 name = models.CharField(max_length=50)
84 image = models.ImageField(upload_to='...')
85
86 class Gallery(models.Model):
87 name = models.CharField(max_length=50)
88 photos = SortedManyToManyField(Photo)
89
90 If you use the relation in your code like the following, it will remember the
91 order in which you have added photos to the gallery. ::
92
93 gallery = Gallery.objects.create(name='Photos ordered by name')
94 for photo in Photo.objects.order_by('name'):
95 gallery.photos.add(photo)
96
97 ``SortedManyToManyField``
98 -------------------------
99
100 You can use the following arguments to modify the default behavior:
101
102 ``sorted``
103 ~~~~~~~~~~
104
105 **Default:** ``True``
106
107 You can set the ``sorted`` to ``False`` which will force the
108 ``SortedManyToManyField`` in behaving like Django's original
109 ``ManyToManyField``. No ordering will be performed on relation nor will the
110 intermediate table have a database field for storing ordering information.
111
112 ``sort_value_field_name``
113 ~~~~~~~~~~~~~~~~~~~~~~~~~
114
115 **Default:** ``'sort_value'``
116
117 Specifies how the field is called in the intermediate database table by which
118 the relationship is ordered. You can change its name if you have a legacy
119 database that you need to integrate into your application.
120
121 ``base_class``
122 ~~~~~~~~~~~~~~
123
124 **Default:** ``None``
125
126 You can set the ``base_class``, which is the base class of the through model of
127 the sortedm2m relationship between models to an abstract base class containing
128 a ``__str__`` method to improve the string representations of sortedm2m
129 relationships.
130
131 .. note::
132
133 You also could use it to add additional fields to the through model. But
134 please beware: These fields will not be created or modified by an
135 automatically created migration. You will need to take care of migrations
136 yourself. In most cases when you want to add another field, consider
137 *not* using sortedm2m but use a ordinary Django ManyToManyField and
138 specify `your own through model`_.
139
140 .. _your own through model: https://docs.djangoproject.com/en/1.11/ref/models/fields/#django.db.models.ManyToManyField.through
141
142 Migrating a ``ManyToManyField`` to be a ``SortedManyToManyField``
143 =================================================================
144
145 If you are using Django's migration framework and want to change a
146 ``ManyToManyField`` to be a ``SortedManyToManyField`` (or the other way
147 around), you will find that a migration created by Django's ``makemigrations``
148 will not work as expected.
149
150 In order to migrate a ``ManyToManyField`` to a ``SortedManyToManyField``, you
151 change the field in your models to be a ``SortedManyToManyField`` as
152 appropriate and create a new migration with ``manage.py makemigrations``.
153 Before applying it, edit the migration file and change in the ``operations``
154 list ``migrations.AlterField`` to ``AlterSortedManyToManyField`` (import it
155 from ``sortedm2m.operations``). This operation will take care of changing the
156 intermediate tables, add the ordering field and fill in default values.
157
158 Admin
159 =====
160
161 ``SortedManyToManyField`` provides a custom widget which can be used to sort
162 the selected items. It renders a list of checkboxes that can be sorted by
163 drag'n'drop.
164
165 To use the widget in the admin you need to add ``sortedm2m`` to your
166 INSTALLED_APPS settings, like::
167
168 INSTALLED_APPS = (
169 'django.contrib.auth',
170 'django.contrib.contenttypes',
171 'django.contrib.sessions',
172 'django.contrib.sites',
173 'django.contrib.messages',
174 'django.contrib.staticfiles',
175 'django.contrib.admin',
176
177 'sortedm2m',
178
179 '...',
180 )
181
182 Otherwise it will not find the css and js files needed to sort by drag'n'drop.
183
184 Finally, make sure *not* to have the model listed in any ``filter_horizontal``
185 or ``filter_vertical`` tuples inside of your ``ModelAdmin`` definitions.
186
187 If you did it right, you'll wind up with something like this:
188
189 .. image:: http://i.imgur.com/HjIW7MI.jpg
190
191 It's also possible to use the ``SortedManyToManyField`` with admin's
192 ``raw_id_fields`` option in the ``ModelAdmin`` definition. Add the name of the
193 ``SortedManyToManyField`` to this list to get a simple text input field. The
194 order in which the ids are entered into the input box is used to sort the
195 items of the sorted m2m relation.
196
197 Example::
198
199 from django.contrib import admin
200
201 class GalleryAdmin(admin.ModelAdmin):
202 raw_id_fields = ('photos',)
203
204 Contribute
205 ==========
206 This is a `Jazzband <https://jazzband.co>`_ project. By contributing you agree to abide by the
207 `Contributor Code of Conduct <https://jazzband.co/about/conduct>`_ and follow the
208 `guidelines <https://jazzband.co/about/guidelines>`_.
209
210 You can find the latest development version on Github_. Get there and fork it, file bugs or send well wishes.
211
212 .. _github: http://github.com/jazzband/django-sortedm2m
213
214 Running the tests
215 -----------------
216
217 I recommend to use ``tox`` to run the tests for all relevant python versions
218 all at once. Therefore install ``tox`` with ``pip install tox``, then type in
219 the root directory of the ``django-sortedm2m`` checkout::
220
221 tox
222
223 The tests are run against SQLite, then against PostgreSQL, then against mySQL -
224 so you need to install PostgreSQL and mySQL on your dev environment, and should
225 have a role/user ``sortedm2m`` set up for both PostgreSQL and mySQL.
226
227 Code Quality
228 ------------
229 This project uses `isort <https://github.com/timothycrosley/isort>`_, `pycodestyle <https://github.com/PyCQA/pycodestyle>`_,
230 and `pylint <https://www.pylint.org>`_ to manage validate code quality. These validations can be run with the
231 following command::
232
233 tox -e quality
234
235
236 Changelog
237 =========
238
239 3.1.1
240 -----
241 * `#191`_: Fixed JS bug for popup in Django admin
242
243 .. _#191: https://github.com/jazzband/django-sortedm2m/pull/191
244
245 3.1.0
246 -----
247 * `#178`_: Bug fixes
248 * `#178`_: Fixed Pylint error
249 * `#175`_: Fixed jQuery UI error
250 * `#183`_: Migrated to GitHub Actions for CI
251 * `#184`_: Added support for Django 3.2
252
253 .. _#170: https://github.com/jazzband/django-sortedm2m/pull/170
254 .. _#178: https://github.com/jazzband/django-sortedm2m/pull/178
255 .. _#175: https://github.com/jazzband/django-sortedm2m/pull/175
256 .. _#183: https://github.com/jazzband/django-sortedm2m/pull/183
257 .. _#184: https://github.com/jazzband/django-sortedm2m/pull/184
258
259 3.0.2
260 -----
261 * `#168`_: Restored `admin/js/jquery.init.js`
262
263 .. _#168: https://github.com/jazzband/django-sortedm2m/pull/168
264
265 3.0.1
266 -----
267 * `#164`_: Added all locales to distributable
268 * `#162`_: Added missing files to MANIFEST.in, and removed .DS_Store files
269 * `#150`_: Added German and Spanish translations
270 * `#149`_: Removed `admin/js/jquery.init.js` from `SortedCheckboxSelectMultiple`
271
272 .. _#164: https://github.com/jazzband/django-sortedm2m/pull/164
273 .. _#162: https://github.com/jazzband/django-sortedm2m/pull/162
274 .. _#150: https://github.com/jazzband/django-sortedm2m/pull/150
275 .. _#149: https://github.com/jazzband/django-sortedm2m/pull/149
276
277 3.0.0
278 -----
279 * `#147`_: Dropped support for Django 2.0
280 * `#152`_: Dropped support for Django 1.10
281 * `#152`_: Add support for Python 3.8
282 * `#152`_: Add support for Django 3.0
283
284 .. _#147: https://github.com/jazzband/django-sortedm2m/issues/147
285 .. _#152: https://github.com/jazzband/django-sortedm2m/issues/152
286
287 2.0.0
288 -----
289 * `#135`_: Updated README with Jazzband details, and added CONTRIBUTING.md
290 * `#136`_: Dropped support for Python 2.6 and 3.3, and Django < 1.11
291 * `#130`_: Added support for Python 3.7 and Django 2.0 to 2.2
292 * `#130`_: Add support of custom through models (only for Django >= 2.2)
293 * `#138`_: Added coverage reporting
294
295 .. _#130: https://github.com/jazzband/django-sortedm2m/issues/130
296 .. _#135: https://github.com/jazzband/django-sortedm2m/pull/135
297 .. _#136: https://github.com/jazzband/django-sortedm2m/pull/136
298 .. _#138: https://github.com/jazzband/django-sortedm2m/pull/138
299
300 1.5.0
301 -----
302
303 * `#101`_: Add support for a custom base class for the many to many intermediate
304 class. See the README for documentation. Thank you Rohith Asrk for the patch.
305 * `#87`_: Fix ``AlterSortedManyToManyField`` operation to support custom set
306 ``_sort_field_name``.
307
308 .. _#101: https://github.com/jazzband/django-sortedm2m/pull/101
309 .. _#87: https://github.com/jazzband/django-sortedm2m/issues/87
310
311 1.4.0
312 -----
313
314 * `#104`_: Add compatibility for Django 1.10 and 1.11!
315 Thank you Frankie Dintino for the patch.
316 * `#94`_: Add french translation files. Mainly for strings in the admin.
317 Thanks to ppython for the patch.
318 * `#93`_: Prevent users from accidentally importing and using
319 ``ManyToManyField`` instead of ``SortedManyToManyField`` from ``sortedm2m``.
320 Thanks Dayne May for the patch.
321
322 .. _#104: https://github.com/jazzband/django-sortedm2m/pull/104
323 .. _#94: https://github.com/jazzband/django-sortedm2m/pull/94
324 .. _#93: https://github.com/jazzband/django-sortedm2m/pull/93
325
326 1.3.3
327 -----
328
329 * `#91`_ & `#92`_: Fix admin widget, when used with Django 1.10. The add a new
330 item opup was not closing. Thanks to Tipuch for the patch.
331
332 .. _#91: https://github.com/jazzband/django-sortedm2m/issues/91
333 .. _#92: https://github.com/jazzband/django-sortedm2m/pull/92
334
335 1.3.2
336 -----
337
338 * `#80`_ & `#83`_: Fix ``SortedMultipleChoiceField.clean`` if the validated
339 value is ``None``. Thanks to Alex Mannhold for the patch.
340
341 .. _#80: https://github.com/jazzband/django-sortedm2m/issues/80
342 .. _#83: https://github.com/jazzband/django-sortedm2m/pull/83
343
344 1.3.1
345 -----
346
347 * `#57`_ & `#81`_: Fix add related object popup error prevents operation when
348 no related objects already exist. Thanks to Vadim Sikora for the fix.
349
350 .. _#57: https://github.com/jazzband/django-sortedm2m/issue/57
351 .. _#81: https://github.com/jazzband/django-sortedm2m/pull/81
352
353 1.3.0
354 -----
355
356 * `#79`_: Use `.sortedm2m-item` selector in the widget's JavaScript code to
357 target the list items. This was previously `ul.sortedm2m li`. This improves
358 compatibility other markup that does not want to use `ul`/`li` tags. Thanks
359 to Michal Dabski for the patch.
360
361 **Note:** If you use custom markup with the JavaScript code, you need to make
362 sure that the items now have the `sortedm2m-item` class name.
363
364 * `#76`_: Add support for to_field_name to SortedMultipleChoiceField. Thanks
365 to Conrad Kramer for the patch.
366
367 .. _#76: https://github.com/jazzband/django-sortedm2m/pull/76
368 .. _#79: https://github.com/jazzband/django-sortedm2m/pull/79
369
370 1.2.2
371 -----
372
373 * `#75`_: Fix "add another" admin popup. It didn't refresh the list of items in Django
374 1.8+. Thanks to Vadim Sikora for the patch.
375
376 .. _#75: https://github.com/jazzband/django-sortedm2m/pull/75
377
378 1.2.1
379 -----
380
381 * *skipped*
382
383 1.2.0
384 -----
385
386 * Dropping Python 3.2 support. It has reached end of life in February 2016.
387
388 1.1.2
389 -----
390
391 * `#71`_: Don't break collectstatic for some cases. Therefore we removed the
392 STATIC_URL prefix from the form media definition in
393 ``SortedCheckboxSelectMultiple``. Thanks to Kirill Ermolov for the
394 patch.
395
396 .. _#71: https://github.com/jazzband/django-sortedm2m/issues/71
397
398 1.1.1
399 -----
400
401 * `#70`_: CSS fix for Django 1.9 new admin design. Thanks to Maarten Draijer
402 for the patch.
403
404 .. _#70: https://github.com/jazzband/django-sortedm2m/pull/70
405
406 1.1.0
407 -----
408
409 * `#59`_, `#65`_, `#68`_: Django 1.9 support. Thanks to Scott Kyle and Jasper Maes for
410 patches.
411 * `#67`_: Support for disabling migrations for some models, that can be
412 decided by Django's DB router (with the ``allow_migrate_model`` method).
413 Thanks to @hstanev for the patch.
414
415 .. _#59: https://github.com/jazzband/django-sortedm2m/pull/59
416 .. _#65: https://github.com/jazzband/django-sortedm2m/pull/65
417 .. _#67: https://github.com/jazzband/django-sortedm2m/pull/67
418 .. _#68: https://github.com/jazzband/django-sortedm2m/pull/68
419
420 1.0.2
421 -----
422
423 * `#56`_: Fix bug where order is wrong after adding objects. That had to do
424 with using the ``count`` of the m2m objects for the next ``sort_value``
425 value. We now use the corret ``Max`` aggregation to make sure that newly
426 added objects will be in order. Thanks to Scott Kyle for the report and
427 patch.
428
429 .. _#56: https://github.com/jazzband/django-sortedm2m/pull/56
430
431 1.0.1
432 -----
433
434 * Performance fix for sorted m2m admin widget. See `#54`_ for details. Thanks
435 to Jonathan Liuti for fixing this.
436
437 .. _#54: https://github.com/jazzband/django-sortedm2m/pull/54
438
439 1.0.0
440 -----
441
442 * Hooray, we officially declare **django-sortedm2m** to be stable and
443 promise to be backwards compatible to new releases (we already doing good
444 since since the beginning in that regard).
445 * Django 1.8 support for ``AlterSortedManyToManyField`` operation. Thanks to
446 Nicolas Trésegnie for starting the implementation.
447
448 0.10.0
449 ------
450
451 * The creation of the sortedm2m intermediate model and database table is now
452 fully done inside of the ``SortedManyToManyField`` class. That makes it much
453 easier to modify the creation of this when creating a custom subclass of this
454 field. See `#49`_ for an example usecase.
455 * Adding support for the custom field arguments like ``sorted`` and
456 ``sort_value_field_name`` in Django 1.7 migrations. Thanks to Christian
457 Kohlstedde for the patch.
458
459 .. _#49: https://github.com/jazzband/django-sortedm2m/issues/49
460
461 0.9.5
462 -----
463
464 * Fixing ``setup.py`` when run on a system that does not use UTF-8 as default
465 encoding. See `#48`_ for details. Thanks to Richard Mitchell for the patch.
466
467 .. _#48: https://github.com/jazzband/django-sortedm2m/pull/48
468
469 0.9.4
470 -----
471
472 * Fix: ``SortedMultipleChoiceField`` did not properly report changes of the
473 data to ``Form.changed_data``. Thanks to @smcoll for the patch.
474
475 0.9.3
476 -----
477
478 * Fix: ``AlterSortedManyToManyField`` operation failed for postgres databases.
479 * Testing against MySQL databases.
480
481 0.9.2
482 -----
483
484 * Fix: ``AlterSortedManyToManyField`` operation failed for many to many fields
485 which already contained some data.
486
487 0.9.1
488 -----
489
490 * Fix: When using the sortable admin widget, deselecting an item in the list
491 had not effect. Thank you to madEng84 for the report and patch!
492
493 0.9.0
494 -----
495
496 * Adding ``AlterSortedManyToManyField`` migration operation that allows you to
497 migrate from ``ManyToManyField`` to ``SortedManyToManyField`` and vice
498 versa. Thanks to Joaquín Pérez for the patch!
499 * Fix: Supporting migrations in Django 1.7.4.
500 * Fix: The admin widget is not broken anymore for dynamically added inline
501 forms. Thanks to Rubén Díaz for the patch!
502
503 0.8.1
504 -----
505
506 * Adding support for Django 1.7 migrations. Thanks to Patryk Hes and Richard
507 Barran for their reports.
508 * Adding czech translations. Thanks to @cuchac for the pull request.
509
510 0.8.0
511 -----
512
513 * Adding support for Django 1.7 and dropping support for Django 1.4.
514
515 0.7.0
516 -----
517
518 * Adding support for ``prefetch_related()``. Thanks to Marcin Ossowski for
519 the idea and patch.
520
521 0.6.1
522 -----
523
524 * Correct escaping of *for* attribute in label for the sortedm2m widget. Thanks
525 to Mystic-Mirage for the report and fix.
526
527 0.6.0
528 -----
529
530 * Python 3 support!
531 * Better widget. Thanks to Mike Knoop for the initial patch.
532
533 0.5.0
534 -----
535
536 * Django 1.5 support. Thanks to Antti Kaihola for the patches.
537 * Dropping Django 1.3 support. Please use django-sortedm2m<0.5 if you need to
538 use Django 1.3.
539 * Adding support for a ``sort_value_field_name`` argument in
540 ``SortedManyToManyField``. Thanks to Trey Hunner for the idea.
541
542 0.4.0
543 -----
544
545 * Django 1.4 support. Thanks to Flavio Curella for the patch.
546 * south support is only enabled if south is actually in your INSTALLED_APPS
547 setting. Thanks to tcmb for the report and Florian Ilgenfritz for the patch.
548
549 0.3.3
550 -----
551
552 * South support (via monkeypatching, but anyway... it's there!). Thanks to
553 Chris Church for the patch. South migrations won't pick up a changed
554 ``sorted`` argument though.
555
556 0.3.2
557 -----
558
559 * Use already included jQuery version in global scope and don't override with
560 django's version. Thank you to Hendrik van der Linde for reporting this
561 issue.
562
563 0.3.1
564 -----
565
566 * Fixed packaging error.
567
568 0.3.0
569 -----
570
571 * Heavy internal refactorings. These were necessary to solve a problem with
572 ``SortedManyToManyField`` and a reference to ``'self'``.
573
574 0.2.5
575 -----
576
577 * Forgot to exclude debug print/console.log statements from code. Sorry.
578
579 0.2.4
580 -----
581
582 * Fixing problems with ``SortedCheckboxSelectMultiple`` widget, especially in
583 admin where a "create and add another item" popup is available.
584
585 0.2.3
586 -----
587
588 * Fixing issue with primary keys instead of model instances for ``.add()`` and
589 ``.remove()`` methods in ``SortedRelatedManager``.
590
591 0.2.2
592 -----
593
594 * Fixing validation error for ``SortedCheckboxSelectMultiple``. It caused
595 errors if only one value was passed.
596
597 0.2.1
598 -----
599
600 * Removed unnecessary reference of jquery ui css file in
601 ``SortedCheckboxSelectMultiple``. Thanks to Klaas van Schelven and Yuwei Yu
602 for the hint.
603
604 0.2.0
605 -----
606
607 * Added a widget for use in admin.
608
609