Ticket #11651: customize_list_editable2.diff

File customize_list_editable2.diff, 3.1 KB (added by adamv, 6 years ago)

Now with docs.

  • django/contrib/admin/options.py

    diff --git a/django/contrib/admin/options.py b/django/contrib/admin/options.py
    index 31a28cc..36cfefd 100644
    a b class BaseModelAdmin(object): 
    6767
    6868    def __init__(self):
    6969        self.formfield_overrides = dict(FORMFIELD_FOR_DBFIELD_DEFAULTS, **self.formfield_overrides)
     70       
     71    def formfield_changelist_for_dbfield(self, db_field, **kwargs):
     72        """
     73        Hook for specifying the form Field instance for a given database Field
     74        instance when using 'list_editable' on the changelist.
     75       
     76        By default, returns formfield_for_dbfield.
     77        """
     78        return self.formfield_for_dbfield(db_field, **kwargs)
    7079
    7180    def formfield_for_dbfield(self, db_field, **kwargs):
    7281        """
    class ModelAdmin(BaseModelAdmin): 
    352361        Returns a Form class for use in the Formset on the changelist page.
    353362        """
    354363        defaults = {
    355             "formfield_callback": curry(self.formfield_for_dbfield, request=request),
     364            "formfield_callback": curry(self.formfield_changelist_for_dbfield, request=request),
    356365        }
    357366        defaults.update(kwargs)
    358367        return modelform_factory(self.model, **defaults)
    class ModelAdmin(BaseModelAdmin): 
    363372        is used.
    364373        """
    365374        defaults = {
    366             "formfield_callback": curry(self.formfield_for_dbfield, request=request),
     375            "formfield_callback": curry(self.formfield_changelist_for_dbfield, request=request),
    367376        }
    368377        defaults.update(kwargs)
    369378        return modelformset_factory(self.model,
  • docs/ref/contrib/admin/index.txt

    diff --git a/docs/ref/contrib/admin/index.txt b/docs/ref/contrib/admin/index.txt
    index 584672e..1ade4f4 100644
    a b you can pass a ``cacheable=True`` argument to :meth:`AdminSite.admin_view`:: 
    797797
    798798    (r'^my_view/$', self.admin_site.admin_view(self.my_view, cacheable=True))
    799799
     800.. method:: ModelAdmin.formfield_for_dbfield(self, db_field, **kwargs)
     801
     802The ``formfield_for_dbfield`` method on a ``ModelAdmin`` allows you to
     803override the default formfield used for a given database field.
     804
     805.. method:: ModelAdmin.formfield_changelist_for_dbfield(self, db_field, **kwargs)
     806
     807.. versionadded:: 1.2
     808
     809The ``formfield_changelist_for_dbfield`` method on a ``ModelAdmin`` allows you to
     810override the default formfields used on a changelist page when list_editable is enabled.
     811Use this method to override a formfield only on the changelist. For example, to set
     812a CSS class for certain fields::
     813
     814    class MyModelAdmin(admin.ModelAdmin):
     815        def formfield_changelist_for_dbfield(self, db_field, **kwargs):
     816            field = super(MyModelAdmin, self).formfield_changelist_for_dbfield(db_field, **kwargs)
     817            if db_field.name in ('short_field_1', 'short_field_2'):
     818                field.widget.attrs['class']='smaller'
     819            return field
     820
     821By default, this method just returns the result of ``formfield_for_dbfield``.
     822
    800823.. method:: ModelAdmin.formfield_for_foreignkey(self, db_field, request, **kwargs)
    801824
    802825.. versionadded:: 1.1
Back to Top