Ticket #15208: 15208_formfield_for_choice_field_doc.diff

File 15208_formfield_for_choice_field_doc.diff, 1.6 KB (added by Julien Phalip, 14 years ago)
  • docs/ref/contrib/admin/index.txt

    diff --git a/docs/ref/contrib/admin/index.txt b/docs/ref/contrib/admin/index.txt
    index 2815b5d..a10c046 100644
    a b templates used by the :class:`ModelAdmin` views:  
    927927                    kwargs["queryset"] = Car.objects.filter(owner=request.user)
    928928                return super(MyModelAdmin, self).formfield_for_manytomany(db_field, request, **kwargs)
    929929
     930.. method:: ModelAdmin.formfield_for_choice_field(self, db_field, request, **kwargs)
     931
     932    Similarly as the ``formfield_for_foreignkey`` and ``formfield_for_manytomany``
     933    methods, the ``formfield_for_choice_field`` method can be overridden to
     934    change the default formfield for a field that has declared choices. For example,
     935    if the choices available to a superuser should be different than those
     936    available to regular staff, you could proceed as follows::
     937   
     938        class MyModelAdmin(admin.ModelAdmin):
     939            def formfield_for_choice_field(self, db_field, request, **kwargs):
     940                if db_field.name == "status":
     941                    kwargs['choices'] = (
     942                        ('accepted', 'Accepted'),
     943                        ('denied', 'Denied'),
     944                    )
     945                    if request.user.is_superuser:
     946                        kwargs['choices'] += (('ready', 'Ready for deployment'),)
     947                return super(MyModelAdmin, self).formfield_for_choice_field(db_field, request, **kwargs)
     948
    930949.. method:: ModelAdmin.has_add_permission(self, request)
    931950
    932951    Should return ``True`` if adding an object is permitted, ``False``
Back to Top