add syntax coloring to README.md

master
Ekin Ertaç 2016-01-28 14:37:45 +02:00
parent 1afbc08704
commit f25c7efa1c
1 changed files with 166 additions and 134 deletions

View File

@ -105,6 +105,7 @@ class MySortableClass(SortableMixin):
#### Common Use Case
A common use case is to have child objects that are sortable relative to a parent. If your parent object is also sortable, here's how you would set up your models and admin options:
```python
# models.py
from adminsortable.fields import SortableForeignKey
@ -138,10 +139,11 @@ A common use case is to have child objects that are sortable relative to a paren
admin.site.register(Category, SortableAdmin)
admin.site.register(Project, SortableAdmin)
```
Sometimes you might have a parent model that is not sortable, but has child models that are. In that case define your models and admin options as such:
```python
from adminsortable.fields import SortableForeignKey
# models.py
@ -178,6 +180,7 @@ Sometimes you might have a parent model that is not sortable, but has child mode
inlines = [ProjectInline]
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.
@ -187,6 +190,7 @@ If you previously used Django Admin Sortable, **DON'T PANIC** - everything will
Please note however that the `Sortable` class still contains the hard-coded `order` field, and meta inheritance requirements:
```python
# legacy model definition
from adminsortable.models import Sortable
@ -198,13 +202,15 @@ Please note however that the `Sortable` class still contains the hard-coded `ord
def __unicode__(self):
return self.title
```
#### Model Instance Methods
Each instance of a sortable model has two convenience methods to get the next or previous instance:
```python
.get_next()
.get_previous()
```
By default, these methods will respect their order in relation to a `SortableForeignKey` field, if present. Meaning, that given the following data:
@ -221,12 +227,15 @@ By default, these methods will respect their order in relation to a `SortableFor
If you wish to override this behavior, pass in: `filter_on_sortable_fk=False`:
```python
your_instance.get_next(filter_on_sortable_fk=False)
```
You may also pass in additional ORM "extra_filters" as a dictionary, should you need to:
```python
your_instance.get_next(extra_filters={'title__icontains': 'blue'})
```
### Adding Sorting to an existing model
@ -235,10 +244,12 @@ If you're adding Sorting to an existing model, it is recommended that you use [d
Example assuming a model named "Category":
```python
def forwards(self, orm):
for index, category in enumerate(orm.Category.objects.all()):
category.order = index + 1
category.save()
```
See: [this link](http://south.readthedocs.org/en/latest/tutorial/part3.html) for more
information on South Data Migrations.
@ -250,6 +261,8 @@ Since schema migrations are built into Django 1.7, you don't have to use south,
### Django Admin Integration
To enable sorting in the admin, you need to inherit from `SortableAdmin`:
```python
from django.contrib import admin
from myapp.models import MySortableClass
from adminsortable.admin import SortableAdmin
@ -258,35 +271,39 @@ To enable sorting in the admin, you need to inherit from `SortableAdmin`:
"""Any admin options you need go here"""
admin.site.register(MySortableClass, MySortableAdminClass)
```
To enable sorting on TabularInline models, you need to inherit from
SortableTabularInline:
```python
from adminsortable.admin import SortableTabularInline
class MySortableTabularInline(SortableTabularInline):
"""Your inline options go here"""
```
To enable sorting on StackedInline models, you need to inherit from
SortableStackedInline:
```python
from adminsortable.admin import SortableStackedInline
class MySortableStackedInline(SortableStackedInline):
"""Your inline options go here"""
```
There are also generic equivalents that you can inherit from:
```python
from adminsortable.admin import (SortableGenericTabularInline,
SortableGenericStackedInline)
"""Your generic inline options go here"""
```
If your parent model is *not* sortable, but has child inlines that are, your parent model needs to inherit from `NonSortableParentAdmin`:
```python
from adminsortable.admin import (NonSortableParentAdmin,
SortableTabularInline)
@ -295,7 +312,7 @@ If your parent model is *not* sortable, but has child inlines that are, your par
class ParentAdmin(NonSortableParentAdmin):
inlines = [ChildTabularInline]
```
#### Overriding `queryset()`
django-admin-sortable supports custom queryset overrides on admin models
@ -312,6 +329,7 @@ an admin class with a custom `queryset()` override.
This is a special case, which requires a few lines of extra code to properly
determine the sortability of your model. Example:
```python
# add this import to your admin.py
from adminsortable.utils import get_is_sortable
@ -331,6 +349,7 @@ determine the sortability of your model. Example:
else:
self.model.is_sortable = False
return qs
```
If you override the queryset of an inline, the number of objects present
may change, and adminsortable won't be able to automatically determine
@ -348,6 +367,7 @@ django-admin-sortable 1.6.6 introduced a backwards-incompatible change for `sort
An example of sorting subsets would be a "Board of Directors". In this use case, you have a list of "People" objects. Some of these people are on the Board of Directors and some not, and you need to sort them independently.
```python
class Person(Sortable):
class Meta(Sortable.Meta):
verbose_name_plural = 'People'
@ -363,6 +383,7 @@ An example of sorting subsets would be a "Board of Directors". In this use case,
def __unicode__(self):
return '{} {}'.format(self.first_name, self.last_name)
```
#### Extending custom templates
By default, adminsortable's change form and change list views inherit from
@ -372,13 +393,17 @@ inline models that are sortable for example.
SortableAdmin has two attributes you can override for this use case:
```python
change_form_template_extends
change_list_template_extends
```
These attributes have default values of:
```python
change_form_template_extends = 'admin/change_form.html'
change_list_template_extends = 'admin/change_list.html'
```
If you need to extend the inline change form templates, you'll need to select the right one, depending on your version of Django. For Django 1.5.x or below, you'll need to extend one of the following:
@ -402,6 +427,7 @@ include the necessary JavaScript for django-admin-sortable to work. Fortunately,
this is easy to resolve, as the `CMSPlugin` class allows a change form template to be
specified:
```python
# example plugin
from cms.plugin_base import CMSPluginBase
@ -421,10 +447,12 @@ specified:
return context
plugin_pool.register_plugin(CMSCarouselPlugin)
```
The contents of `sortable-stacked-inline-change-form.html` at a minimum need to extend
the extrahead block with:
```html
{% extends "admin/cms/page/plugin_change_form.html" %}
{% load static from staticfiles %}
@ -436,15 +464,19 @@ the extrahead block with:
<link rel="stylesheet" type="text/css" href="{% static 'adminsortable/css/admin.sortable.inline.css' %}" />
{% endblock extrahead %}
```
Sorting within Django-CMS is really only feasible for inline models of a plugin as Django-CMS already includes sorting for plugin instances. For tabular inlines, just substitute:
```html
<script src="{% static 'adminsortable/js/admin.sortable.stacked.inlines.js' %}"></script>
```
with:
```html
<script src="{% static 'adminsortable/js/admin.sortable.tabular.inlines.js' %}"></script>
```
### Rationale
Other projects have added drag-and-drop ordering to the ChangeList