Commits

Simon Meers committed bdc3411

ChangeLinkInline and ChangeFormOnlyAdmin

Comments (0)

Files changed (1)

generic/admin/mixins.py

                         self.model._meta.module_name))
                 ) + urls
         return urls
+
+
+class ChangeLinkInline(admin.TabularInline):
+    """
+    Base class for inlines which link to change forms for further editing.
+
+    Simple workaround for "deep" admin interfaces which would otherwise
+    require "nested inlines". See https://code.djangoproject.com/ticket/9025
+
+    Generally useful in conjunction with ChangeFormOnlyAdmin below.
+    """
+
+    readonly_fields = ('change_link',)
+
+    def change_link(self, obj):
+        if obj.id is None:
+            return 'Not yet saved'
+        return '<a href="%s">Click to edit</a>' % reverse(
+            'admin:%s_%s_change' % (
+                obj._meta.app_label,
+                obj._meta.module_name,
+            ),
+            args=(obj.id,)
+        )
+    change_link.allow_tags = True
+    change_link.short_description = 'Edit'
+
+
+class ChangeFormOnlyAdmin(admin.ModelAdmin):
+    """
+    For models which don't require an independent change list.
+
+    Generally used with ChangeLinkInline (above) where the inlines on the
+    parent object basically act as the change-list.
+    """
+    def has_change_permission(self, request, obj=None):
+        if obj is None:
+            return False
+        return super(
+            ChangeFormOnlyAdmin, self).has_change_permission(request, obj)
+
+    # TODO: rewrite/redirect change_list links in breadcrumbs, etc
+
+    def has_add_permission(self, request):
+        return False
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.