Skip to content

Commit

Permalink
Use current_app for admin list linked fields
Browse files Browse the repository at this point in the history
  • Loading branch information
darklow committed Mar 23, 2017
1 parent b3e24be commit 1dd776b
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 11 deletions.
9 changes: 5 additions & 4 deletions demo/demo/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from django_select2.forms import ModelSelect2Widget
from suit import apps

from suit.admin import RelatedFieldAdmin, getter_for_related_field
from suit.admin import RelatedFieldAdmin, get_related_field
from suit.admin_filters import IsNullFieldListFilter
from suit.sortables import SortableTabularInline, SortableModelAdmin, SortableStackedInline
from suit.widgets import AutosizedTextarea, EnclosedInput
Expand Down Expand Up @@ -233,14 +233,15 @@ class ShowcaseAdmin(RelatedFieldAdmin):
suit_list_filter_horizontal = ('choices',)
# list_display = ('name', 'help_text', 'choices', 'horizontal_choices', 'boolean')
list_display = ('name', 'help_text', 'link_to_country__continent')
readonly_fields = ('readonly_field',)
readonly_fields = ('readonly_field', 'link_to_country')
radio_fields = {"horizontal_choices": admin.HORIZONTAL,
'vertical_choices': admin.VERTICAL}
raw_id_fields = ('raw_id_field',)

# Optional: Use following to override short_description or admin_order_field if needed
link_to_country__continent = getter_for_related_field(
link_to_country__continent = get_related_field(
'link_to_country__continent', short_description='Continent (2nd level FK link)')
link_to_country = get_related_field('link_to_country')

fieldsets = [
(None, {'fields': ['name', 'help_text', 'textfield',
Expand All @@ -260,7 +261,7 @@ class ShowcaseAdmin(RelatedFieldAdmin):

('Foreign key relations',
{'description': 'Original select and linked select feature',
'fields': ['country', 'country2', 'raw_id_field']}),
'fields': ['link_to_country', 'country', 'country2', 'raw_id_field']}),

# ('Date and time', {
# 'description': 'Improved date/time widgets (SuitDateWidget, '
Expand Down
26 changes: 19 additions & 7 deletions suit/admin.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from django.contrib import admin
from django.db import models
from django.core.urlresolvers import reverse_lazy

"""
Adapted by using following examples:
Expand All @@ -10,10 +11,20 @@
link_to_prefix = 'link_to_'


def getter_for_related_field(name, short_description=None, admin_order_field=None):
def get_admin_url(instance, admin_prefix='admin', current_app=None):
if not instance.pk:
return
return reverse_lazy(
'%s:%s_%s_change' % (admin_prefix, instance._meta.app_label, instance._meta.model_name),
args=(instance.pk,),
current_app=current_app
)


def get_related_field(name, short_description=None, admin_order_field=None, admin_prefix='admin'):
"""
Create a function that can be attached to a ModelAdmin to use as a list_display field, e.g:
client__name = getter_for_related_field('client__name', short_description='Client')
client__name = get_related_field('client__name', short_description='Client')
"""
as_link = name.startswith(link_to_prefix)
if as_link:
Expand All @@ -26,10 +37,8 @@ def getter(self, obj):
continue
obj = getattr(obj, related_name)
if obj and as_link:
obj = u'<a href="../../%s/%s/%d/" class="link-with-icon">%s<i class="fa fa-caret-right"></i></a>' % (
obj._meta.app_label, obj._meta.model_name,
obj.id, obj
)
obj = u'<a href="%s" class="link-with-icon">%s<i class="fa fa-caret-right"></i></a>' % \
(get_admin_url(obj, admin_prefix, current_app=self.admin_site.name), obj)
return obj

getter.admin_order_field = admin_order_field or name
Expand All @@ -40,13 +49,16 @@ def getter(self, obj):


class RelatedFieldAdminMetaclass(type(admin.ModelAdmin)):
related_field_admin_prefix = 'admin'

def __new__(cls, name, bases, attrs):
new_class = super(RelatedFieldAdminMetaclass, cls).__new__(cls, name, bases, attrs)

for field in new_class.list_display:
if '__' in field or field.startswith(link_to_prefix):
if not hasattr(new_class, field):
setattr(new_class, field, getter_for_related_field(field))
setattr(new_class, field, get_related_field(
field, admin_prefix=cls.related_field_admin_prefix))

return new_class

Expand Down

0 comments on commit 1dd776b

Please sign in to comment.