|
| 1 | +# django-subadmin |
| 2 | + |
| 3 | +`django-subadmin` provides a special kind of `ModelAdmin`, called `SubAdmin`, that allows it to be nested within another `ModelAdmin` instance. Similar to django's built-in `InlineModelAdmin`, it allows editing of related objects, but instead of doing it inline, it gives you a full `ModelAdmin` as sub-admin of parent `ModelAdmin`. Like `InlineModelAdmin` it works on models related by `ForeignKey`. Multiple `SubAdmin` instances can be nested within a single `ModelAdmin` or `SubAdmin` allowing for multi-level nesting. |
| 4 | + |
| 5 | +## Installation |
| 6 | + |
| 7 | +The easiest and recommended way to install `django-subadmin` is from [PyPI](https://pypi.python.org/pypi/django-subadmin) |
| 8 | + |
| 9 | +``` |
| 10 | +pip install django-subadmin |
| 11 | +``` |
| 12 | + |
| 13 | +You need to add `subadmin` to `INSTALLED_APPS` in your projects `settings.py`, otherwise `django` will not be able to find the necessary templates and template tags. |
| 14 | + |
| 15 | +``` |
| 16 | +# settings.py |
| 17 | +
|
| 18 | +INSTALLED_APPS = ( |
| 19 | + ... |
| 20 | + 'subadmin', |
| 21 | + ... |
| 22 | +) |
| 23 | +``` |
| 24 | + |
| 25 | +## Example Usage |
| 26 | + |
| 27 | +Sometimes things are best explained by an example. Let's say you have two related models. |
| 28 | + |
| 29 | +```python |
| 30 | +# models.py |
| 31 | + |
| 32 | +class MailingList(models.Model): |
| 33 | + name = models.CharField(max_length=100) |
| 34 | + |
| 35 | + |
| 36 | +class Subscriber(models.Model): |
| 37 | + mailing_list = models.ForeignKey(MailingList) |
| 38 | + username = models.CharField(max_length=100) |
| 39 | +``` |
| 40 | + |
| 41 | +If you wish to display only subscribers belonging to a particular mailing list in django admin, your only options is to use `InlineModelAdmin`, which is not very practical when dealing with large number of related objects, plus, you loose all the cool functionality of `ModelAdmin` like searching, filtering, pagination, etc ... |
| 42 | + |
| 43 | +This is where `SubAdmin` comes in. |
| 44 | + |
| 45 | +```python |
| 46 | +# admin.py |
| 47 | + |
| 48 | +from subadmin import SubAdmin, RootSubAdmin |
| 49 | +from .models import MailingList, Subscriber |
| 50 | + |
| 51 | +# Instead of admin.ModelAdmin we subclass SubAdmin, |
| 52 | +# we also set model attribute |
| 53 | + |
| 54 | +class SubscriberSubAdmin(SubAdmin): |
| 55 | + model = Subscriber |
| 56 | + list_display = ('username',) |
| 57 | + |
| 58 | + |
| 59 | +# Since this is the top level model admin, which will be registred with admin.site, |
| 60 | +# we subclass RootSubAdmin and set subadmins attribute |
| 61 | + |
| 62 | +class MailingListAdmin(RootSubAdmin): |
| 63 | + list_display = ('name',) |
| 64 | + |
| 65 | + subadmins = [SubscriberSubAdmin] |
| 66 | + |
| 67 | + |
| 68 | +admin.site.register(MailingList, MailingListAdmin) |
| 69 | +``` |
| 70 | + |
| 71 | +With just a few lines of code you get a fully functional `ModelAdmin`, that will automatically pull in just the relevant related objects, based on `ForeignKey` relation between the two models, it will also auto set `ForeignKey` fields for nested relations and exclude them from change form when adding and editing objects on subadmin. |
| 72 | + |
| 73 | +If you want to see it in action, or get a more in-depth look at how to set everything up, check out <https://github.com/inueni/django-subadmin-example>. |
| 74 | + |
| 75 | + |
| 76 | +## Supported Django versions |
| 77 | + |
| 78 | +Current release of `django-subadmin` is **1.9.0** and is compatible with Django 1.9, 1.10 and 1.11. |
| 79 | + |
| 80 | +Since Django versions before 1.11 don't support `get_exclude` on `ModelAdmin` instances, a workaround that temporarily stores excluded fields on `ModelAdmin` instance, is used. This should not cause any issues under normal circumstances. |
| 81 | + |
| 82 | +#### Verison numbering |
| 83 | + |
| 84 | +`django-subadmin` version numbers are related to Django version numbers. `django-subadmin` major and minor version numbers equal the minimal compatible django release. |
| 85 | + |
| 86 | + |
| 87 | +## Stability |
| 88 | + |
| 89 | +`django-subadmin` has evolved from code that has been running on production servers since early 2014 without any issues. Still, the code has been heavily refactored prior to public release, and while it is unlikely to eat your data, consider it **BETA** software. |
0 commit comments