Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added AdminChangeLinksMixin #48

Open
wants to merge 1 commit into
base: django3
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions django_common/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
from django.forms.models import (inlineformset_factory, BaseInlineFormSet)
from django import forms
from .helper import curry
from django.shortcuts import resolve_url
from django.utils.html import format_html
from django.contrib.admin.templatetags.admin_urls import admin_urlname

from django_common.compat import (atomic_decorator, force_unicode,
unquote, flatten_fieldsets)
Expand Down Expand Up @@ -458,3 +461,28 @@ def get_inlines(self, request, obj=None, prefix=None):
nested_inlines.append(nested_inline)

return nested_inlines


class AdminChangeLinksMixin:
change_links = []

def __init__(self, *args, **kwargs):
for field_name in self.change_links:
def func(obj):
field = getattr(obj, field_name)

# noinspection PyProtectedMember
url_on_field_change = format_html(
"<a href='{}'>{}</a>".format(
resolve_url(admin_urlname(field._meta, "change"), field.pk), str(field)
)
)
return url_on_field_change
function_name = "{}_link".format(field_name)

setattr(self, function_name, func)
getattr(self, function_name).short_description = function_name

self.list_display.append(function_name)

super(AdminChangeLinksMixin, self).__init__(*args, **kwargs)