Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

[1.2.X] Fixed #15208 - Document ModelAdmin.formfield_for_choice_field…

…; thanks julien.

Backport of r15399 from trunk.

git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.2.X@15400 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit f6606da3e835bf365550862833408d3733bb4d97 1 parent 01fce67
Tim Graham authored February 02, 2011

Showing 1 changed file with 19 additions and 0 deletions. Show diff stats Hide diff stats

  1. 19  docs/ref/contrib/admin/index.txt
19  docs/ref/contrib/admin/index.txt
@@ -887,6 +887,25 @@ to multiple owners -- a many to many relationship -- you could filter the
887 887
                 kwargs["queryset"] = Car.objects.filter(owner=request.user)
888 888
             return super(MyModelAdmin, self).formfield_for_manytomany(db_field, request, **kwargs)
889 889
 
  890
+.. method:: ModelAdmin.formfield_for_choice_field(self, db_field, request, **kwargs)
  891
+
  892
+    Like the ``formfield_for_foreignkey`` and ``formfield_for_manytomany``
  893
+    methods, the ``formfield_for_choice_field`` method can be overridden to
  894
+    change the default formfield for a field that has declared choices. For
  895
+    example, if the choices available to a superuser should be different than
  896
+    those available to regular staff, you could proceed as follows::
  897
+
  898
+        class MyModelAdmin(admin.ModelAdmin):
  899
+            def formfield_for_choice_field(self, db_field, request, **kwargs):
  900
+                if db_field.name == "status":
  901
+                    kwargs['choices'] = (
  902
+                        ('accepted', 'Accepted'),
  903
+                        ('denied', 'Denied'),
  904
+                    )
  905
+                    if request.user.is_superuser:
  906
+                        kwargs['choices'] += (('ready', 'Ready for deployment'),)
  907
+                return super(MyModelAdmin, self).formfield_for_choice_field(db_field, request, **kwargs)
  908
+
890 909
 .. method:: ModelAdmin.has_add_permission(self, request)
891 910
 
892 911
     Should return ``True`` if adding an object is permitted, ``False``

0 notes on commit f6606da

Please sign in to comment.
Something went wrong with that request. Please try again.