### Mixin for Custom Admin Views Source: https://github.com/ebertti/django-admin-easy/blob/main/README.rst Explains how to use the `MixinEasyViews` to create custom views within your Django admin classes. It includes examples of defining a custom view method and how to reverse URLs for these custom views. ```python from django.contrib import admin import easy from django.http import HttpResponse class MyModelAdmin(easy.MixinEasyViews, admin.ModelAdmin): # ... def easy_view_jump(self, request, pk=None): # do something here return HttpResponse('something') ``` ```python from django.core.urlresolvers import reverse # to do something with one object of a model reverse('admin:myapp_mymodel_easy', args=(obj.pk, 'jump')) # or to do something with a model reverse('admin:myapp_mymodel_easy', args=('jump',)) ``` ```html # {% url 'admin:myapp_mymodel_easy' obj.pk 'jump' %} # {% url 'admin:myapp_mymodel_easy' 'jump' %} ``` -------------------------------- ### Admin Field Rendering Source: https://github.com/ebertti/django-admin-easy/blob/main/README.rst Provides examples of various custom admin field types for displaying data in the Django admin change list. This includes simple fields, boolean fields, formatted strings, foreign keys, raw IDs, templates, links, images, and fields with template filters. ```python # render a value of field, method, property or your model or related model simple1 = easy.SimpleAdminField('model_field') simple2 = easy.SimpleAdminField('method_of_model') simple3 = easy.SimpleAdminField('related.attribute_or_method') simple4 = easy.SimpleAdminField('related_set.count', 'count') simple5 = easy.SimpleAdminField(lambda x: x.method(), 'show', 'order_by') # render boolean fields bool1 = easy.BooleanAdminField(lambda x: x.value > 10, 'high') # render with string format fields format1 = easy.FormatAdminField('{o.model_field} - {o.date_field:Y%-%m}', 'column name') # render foreignkey with link to change_form in admin fk1 = easy.ForeignKeyAdminField('related') # render foreignkey with link to change_form in admin and related_id content as text fk2 = easy.ForeignKeyAdminField('related', 'related_id') # render foreignkey_id, like raw_id_fields, with link to change_form in admin and related_id content as text # without extra queries or select_related to prevent extra n-1 queries raw1 = easy.RawIdAdminField('related') # render template template1 = easy.TemplateAdminField('test.html', 'shorty description', 'order_field') # render to change_list of another model with a filter on query link1 = easy.LinkChangeListAdminField('app_label', 'model_name', 'attribute_to_text', {'field_name':'dynamic_value_model'}) link2 = easy.LinkChangeListAdminField('app_label', 'model_name', 'attribute_to_text', {'field_name':'dynamic_value_model'}, {'another_field': 'static_value'}) # render link to generic content type fields # don't forget to use select_related with content-type to avoid N+1 queries like example below generic = easy.GenericForeignKeyAdminField('generic') def get_queryset(self, request): qs = super().get_queryset(request) return qs.select_related('content_type') # or enable cache generic = easy.GenericForeignKeyAdminField('generic', cache_content_type=True) # display image of some model image1 = easy.ImageAdminField('image', {'image_attrs':'attr_value'}) # use django template filter on a field filter1 = easy.FilterAdminField('model_field', 'upper') filter2 = easy.FilterAdminField('date_field', 'date', 'django', 'y-m-d') filter3 = easy.FilterAdminField('float_field', 'localize', 'l18n') ``` -------------------------------- ### Admin Action Response Utility Source: https://github.com/ebertti/django-admin-easy/blob/main/README.rst Shows how to use the `easy.action_response` utility function to provide feedback to users after performing admin actions. It demonstrates how to display success or error messages and control whether the querystring is kept. ```python from django.contrib import admin from django.contrib import messages import easy class YourAdmin(admin.ModelAdmin): # ... actions = ('simples_action',) def simples_action(self, request, queryset): success = queryset.do_something() if success: return easy.action_response(request, 'Some success message for user', keep_querystring=False) else: return easy.action_response(request, 'Some error for user', messages.ERROR) # or just redirect to changelist with filters return easy.action_response() ``` -------------------------------- ### Smart and Short Admin Field Decorators Source: https://github.com/ebertti/django-admin-easy/blob/main/README.rst Demonstrates the use of @easy.smart and @easy.short decorators for concisely defining custom admin fields with descriptions, ordering, and other attributes. These decorators simplify the process of adding custom display logic to your admin interface. ```python @easy.smart(short_description='Field Description 12', admin_order_field='model_field') def custom12(self, obj): return obj.something_cool() @easy.short(desc='Field Description 1', order='model_field', tags=True) def decorator1(self, obj): return '' + obj.model_field + '' @easy.short(desc='Field Description 2', order='model_field', bool=True) ``` -------------------------------- ### Using Decorators for Computed Fields with django-admin-easy Source: https://github.com/ebertti/django-admin-easy/blob/main/README.rst Illustrates using the `@easy.smart` and `@easy.short` decorators to simplify the definition of computed fields in ModelAdmin. ```python from django.contrib import admin import easy class YourAdmin(admin.ModelAdmin): fields = ('sum_method', 'some_img', 'is_true') @easy.smart(short_description='Sum', admin_order_field='field1', allow_tags=True ) def sum_method(self, obj): sum_result = obj.field1 + obj.field2 + obj.field3 return '%s' % sum_result @easy.short(desc='image', order='id', tags=True) def some_img(self, obj): return '' % obj.image @easy.short(desc='Positive', order='value', bool=True) def is_true(self, obj): return obj.value > 0 ``` -------------------------------- ### Simplified Computed Fields with django-admin-easy Source: https://github.com/ebertti/django-admin-easy/blob/main/README.rst Shows how to use django-admin-easy's SimpleAdminField, ImageAdminField, and BooleanAdminField to create computed fields more concisely. ```python from django.contrib import admin import easy class YourAdmin(admin.ModelAdmin): fields = ('sum_method', 'some_img', 'is_true') sum_method = easy.SimpleAdminField(lambda obj: '%s' % (obj.field1 + obj.field2 + obj.field3), 'Sum', 'field1', True) some_img = easy.ImageAdminField('image', 'id') is_true = easy.BooleanAdminField('Positive', 'value') ``` -------------------------------- ### Django Utility Functions Source: https://github.com/ebertti/django-admin-easy/blob/main/README.rst Shows how to integrate various Django utility functions directly into your admin methods using the @easy.utils decorator. This provides convenient access to common Django helpers for HTML escaping, string manipulation, and internationalization. ```python @easy.utils('html.escape') @easy.utils('html.conditional_escape') @easy.utils('html.strip_tags') @easy.utils('safestring.mark_safe') @easy.utils('safestring.mark_for_escaping') @easy.utils('text.slugify') @easy.utils('translation.gettext') @easy.utils('translation.ugettext') @easy.utils('translation.gettext_lazy') @easy.utils('translation.ugettext_lazy') @easy.utils('translation.gettext_noop') @easy.utils('translation.ugettext_noop') def your_method(self, obj): return obj.value ``` -------------------------------- ### Allowing HTML Tags in Admin Fields Source: https://github.com/ebertti/django-admin-easy/blob/main/README.rst Demonstrates how to use the `@easy.with_tags()` decorator to ensure HTML content is rendered correctly in Django admin fields. ```python import easy @easy.with_tags() def some_field_with_html(self, obj): return '{}'.format(obj.value) ``` -------------------------------- ### Custom Admin Actions Source: https://github.com/ebertti/django-admin-easy/blob/main/README.rst Illustrates how to define custom admin actions using the @easy.action decorator. You can specify a custom name for the action and optionally restrict its availability based on user permissions. ```python @easy.action('My Little Simple Magic Action') def simple_action(self, request, queryset): return queryset.update(magic=True) # actoin only for user that has change permission on this model @easy.action('Another Simple Magic Action', 'change') def simple_action(self, request, queryset): return queryset.update(magic=True) ``` -------------------------------- ### Basic Computed Fields in Django Admin Source: https://github.com/ebertti/django-admin-easy/blob/main/README.rst Demonstrates how to define computed fields directly in ModelAdmin without django-admin-easy. ```python from django.contrib import admin class YourAdmin(admin.ModelAdmin): fields = ('sum_method', 'some_img', 'is_true') def sum_method(self, obj): sum_result = obj.field1 + obj.field2 + obj.field3 return '%s' % sum_result sum_method.short_description = 'Sum' sum_method.admin_order_field = 'field1' sum_method.allow_tags = True def some_img(self, obj): return '' % obj.image some_img.short_description = 'image' some_img.admin_order_field = 'id' some_img.allow_tags = True def is_true(self, obj): return obj.value > 0 is_true.short_description = 'Positive' is_true.admin_order_field = 'value' is_true.boolean = True ``` -------------------------------- ### Caching Custom Admin Fields Source: https://github.com/ebertti/django-admin-easy/blob/main/README.rst Shows how to use the `@easy.cache()` decorator to cache the results of computed admin fields, with an option to specify the cache duration in seconds. ```python import easy @easy.cache(10)# in secondd, default is 60 def some_field_with_html(self, obj): return obj.related.some_hard_word() ``` -------------------------------- ### Custom Template Filters Source: https://github.com/ebertti/django-admin-easy/blob/main/README.rst Demonstrates how to register custom template filters using the @easy.filter decorator. This allows you to apply custom logic to template variables, similar to built-in filters like 'title' or 'localize'. ```python # builtin template filter like {{ value|title }} @easy.filter('title') def some_field_with_html(self, obj): return 'ezequiel bertti' # output: "Ezequiel Bertti" # like {% load i18n %} and {{ value|localize }} @easy.filter('localize', 'l10n') def some_field_with_html(self, obj): return 10000 # output: "10.000" # like {{ value|date:'y-m-d' }} @easy.filter('date', 'default', 'y-m-d') def some_field_with_html(self, obj): return datetime(2016, 06, 28) # output: "16-06-28" ``` -------------------------------- ### Custom Fields in Django Admin Source: https://github.com/ebertti/django-admin-easy/blob/main/README.rst Demonstrates how to use custom fields like ForeignKeyAdminField and TemplateAdminField within your Django admin classes. It also shows how to register these custom fields in `readonly_fields` or `list_fields`. ```python from django.contrib import admin import easy class YourAdmin(admin.ModelAdmin): fields = ('custom1', 'custom2', 'custom3' ... 'customN') readonly_fields = ('custom1', 'custom2', 'custom3' ... 'customN') custom1 = easy.ForeignKeyAdminField('related') # ... class YourAdminListFields(admin.ModelAdmin): list_fields = ( easy.TemplateAdminField('test.html', 'shorty description', 'order_field'), easy.ImageAdminField('image', {'image_attrs':'attr_value'}), # ... ) # ... ``` -------------------------------- ### Clearing Cached Admin Fields Source: https://github.com/ebertti/django-admin-easy/blob/main/README.rst Provides methods to clear the cache for computed admin fields, either globally or within a model's save method. ```python import easy # wherever you want easy.cache_clear(my_model_instance) # or class MyModel(models.Model): # ... fields def save(*args, **kwargs): easy.cache_clear(self) super(MyModel, self).save(*args, **kwargs) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.