Ticket #11651: customize_list_editable3.diff

File customize_list_editable3.diff, 3.2 KB (added by Adam Vandenberg, 15 years ago)

Rebased patch on Django 1.2.

  • django/contrib/admin/options.py

    diff --git a/django/contrib/admin/options.py b/django/contrib/admin/options.py
    index 1f8ff6d..6fe494e 100644
    a b class BaseModelAdmin(object):  
    7373        overrides.update(self.formfield_overrides)
    7474        self.formfield_overrides = overrides
    7575
     76    def formfield_changelist_for_dbfield(self, db_field, **kwargs):
     77        """
     78        Hook for specifying the form Field instance for a given database Field
     79        instance when using 'list_editable' on the changelist.
     80
     81        By default, returns formfield_for_dbfield.
     82        """
     83        return self.formfield_for_dbfield(db_field, **kwargs)
     84
    7685    def formfield_for_dbfield(self, db_field, **kwargs):
    7786        """
    7887        Hook for specifying the form Field instance for a given database Field
    class ModelAdmin(BaseModelAdmin):  
    392401        Returns a Form class for use in the Formset on the changelist page.
    393402        """
    394403        defaults = {
    395             "formfield_callback": curry(self.formfield_for_dbfield, request=request),
     404            "formfield_callback": curry(self.formfield_changelist_for_dbfield, request=request),
    396405        }
    397406        defaults.update(kwargs)
    398407        return modelform_factory(self.model, **defaults)
    class ModelAdmin(BaseModelAdmin):  
    403412        is used.
    404413        """
    405414        defaults = {
    406             "formfield_callback": curry(self.formfield_for_dbfield, request=request),
     415            "formfield_callback": curry(self.formfield_changelist_for_dbfield, request=request),
    407416        }
    408417        defaults.update(kwargs)
    409418        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 7fee903..942c926 100644
    a b you can pass a ``cacheable=True`` argument to :meth:`AdminSite.admin_view`::  
    853853
    854854    (r'^my_view/$', self.admin_site.admin_view(self.my_view, cacheable=True))
    855855
     856.. method:: ModelAdmin.formfield_for_dbfield(self, db_field, **kwargs)
     857
     858The ``formfield_for_dbfield`` method on a ``ModelAdmin`` allows you to
     859override the default formfield used for a given database field.
     860
     861.. method:: ModelAdmin.formfield_changelist_for_dbfield(self, db_field, **kwargs)
     862
     863.. versionadded:: 1.3
     864
     865The ``formfield_changelist_for_dbfield`` method on a ``ModelAdmin`` allows you to
     866override the default formfields used on a changelist page when list_editable is enabled.
     867Use this method to override a formfield only on the changelist. For example, to set
     868a CSS class for certain fields::
     869
     870    class MyModelAdmin(admin.ModelAdmin):
     871        def formfield_changelist_for_dbfield(self, db_field, **kwargs):
     872            field = super(MyModelAdmin, self).formfield_changelist_for_dbfield(db_field, **kwargs)
     873            if db_field.name in ('short_field_1', 'short_field_2'):
     874                field.widget.attrs['class']='smaller'
     875            return field
     876
     877By default, this method just returns the result of ``formfield_for_dbfield``.
     878
    856879.. method:: ModelAdmin.formfield_for_foreignkey(self, db_field, request, **kwargs)
    857880
    858881.. versionadded:: 1.1
Back to Top