A simple jQuery javascript that collapses all stacked inline rows for better handling of large inline fieldsets.
It also adds "Show"/"Hide"-buttons for showing/hiding each row, which could be customized and styled using css.
**Usage (see below for example):**
Include the javascript on your admin page, together with jQuery, and it'll automatically affect all stacked inlines.
**Works with**
Django 3.1.4
(Might work with other versions with or without adjustments, but not tested)
**Use example:**
*admin.py:*
class DateInline(admin.StackedInline):
model = Date
extra = 10
class EventAdmin(admin.ModelAdmin):
inlines = [DateInline]
class Media:
js = ['js/collapsed-stacked-inlines.js']
admin.site.register(Event, EventAdmin)
Based on https://djangosnippets.org/snippets/2020/ and https://stackoverflow.com/questions/5146539/streaming-a-csv-file-in-django
Can be used on really large querysets.
This snippets is inspired from [#2995](https://djangosnippets.org/snippets/2995/) but add the following:
* get rid of `singledispatch` so we can use it with python2 without pip installing anything
* streaming capabilities
* the configuration params (header, fields, exclude...) are passed to the action function so we don't pollute the ModelAdmin class
* fix utf8 issue in header
Implements necessary permission checks on a user model to be compatible with django admin, but just return true on all permissions without actually checking it against anything. Useful when you have a user model that should always be allowed to use django admin, and you don't care about using django's own PermissionsMixin and don't want to have those models added to your database.
Save the snippet as actions.py within your core app, and then add an action on any model you want in it's ModelAdmin definition.
Example usage:
`from actions import export_as_csv_action`
`class YourModelAdmin(admin.ModelAdmin):`
` list_display = ('field', 'get_field2')`
` actions = [export_to_csv(filename='your-model')]`
` def get_field2(self, obj):`
` return obj.field2`
Usage :
class MyModelAdmin(ReadonlyLinksMixin, admin.ModelAdmin):
readonly_fields_links = ('field1', 'field2')
This adds a new ModelAdmin property (`readonly_fields_links`) that acts like the default `readonly_links` except that (if the field's type is a model that can be edited in the admin site) the value of the field has a link to the object.
Same functionality as
* [This snippet](https://www.djangosnippets.org/snippets/937/)
* [and this one](https://www.djangosnippets.org/snippets/1008/)
Except that it works without messing with the form that gets validated and saved, and thus without sometimes saving None values. It uses the documented property that `readonly_fields` can be callables ([Django doc](https://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.list_display)) : the fields in `readonly_links_fields` are turned into callables that are appended to `readonly_links`. Each callable creates the linked value.
The idea here is to wrap the original `delete_selected` functionality in a way that I shouldn't have to reimplement the templates (confirmation/error response) serving, just extend the original.
What this code does, it wraps the queryset's delete function with a closure, so when it really gets called (after the confirmation), it executes the extra functionality you wish to.
After looking at the original code, this seemed to be the most efficient way of doing it.
## How to use
Use this [admin filter](https://docs.djangoproject.com/en/1.8/ref/contrib/admin/#django.contrib.admin.ModelAdmin.list_filter) together with a numeric field to allow filtering changlist by field
values range (in this case, age groups):
For example, to group customers by age groups:
class Customer(models.Model):
# ...
age = models.IntegerField()
age.list_lookup_range = (
(None, _('All')),
([0, 2], '0-2'),
([2, 4], '2-4'),
([4, 18], '4-18'),
([18, 65], '18-65'),
([65, None], '65+'),
))
class CustomerAdmin(admin.ModelAdmin):
list_filter = [('age', ValueRangeFilter), ]
## Inspiration
[This snippet](https://djangosnippets.org/snippets/587/) (for django < 1.4) inspired me to make this work for newer django versions.
Sometimes you just don't want to display every permission django has, you just want a short list showing the permissions for some of your apps (or even django core apps).
GROUP_PERMISSIONS_MODELS must be a list of your app models. Dotted path (in lowercase) required, app name + model *class* name.