Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #11882 -- Added documentation for formfield_for_manytomany. Tha…

…nks to Rob Hudson, timo and Simon Meers for their work on the patch.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@13552 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 6ac4aba918fde4d0de8cb371d4ae72cd09fa7151 1 parent 7aa7943
Russell Keith-Magee authored August 07, 2010

Showing 1 changed file with 17 additions and 2 deletions. Show diff stats Hide diff stats

  1. 19  docs/ref/contrib/admin/index.txt
19  docs/ref/contrib/admin/index.txt
@@ -869,11 +869,26 @@ return a subset of objects for this foreign key field based on the user::
869 869
         def formfield_for_foreignkey(self, db_field, request, **kwargs):
870 870
             if db_field.name == "car":
871 871
                 kwargs["queryset"] = Car.objects.filter(owner=request.user)
872  
-                return db_field.formfield(**kwargs)
873 872
             return super(MyModelAdmin, self).formfield_for_foreignkey(db_field, request, **kwargs)
874 873
 
875 874
 This uses the ``HttpRequest`` instance to filter the ``Car`` foreign key field
876  
-to only the cars owned by the ``User`` instance.
  875
+to only display the cars owned by the ``User`` instance.
  876
+
  877
+.. method:: ModelAdmin.formfield_for_manytomany(self, db_field, request, **kwargs)
  878
+
  879
+.. versionadded:: 1.1
  880
+
  881
+Like the ``formfield_for_foreignkey`` method, the ``formfield_for_manytomany``
  882
+method can be overridden to change the default formfield for a many to many
  883
+field. For example, if an owner can own multiple cars and cars can belong
  884
+to multiple owners -- a many to many relationship -- you could filter the
  885
+``Car`` foreign key field to only display the cars owned by the ``User``::
  886
+
  887
+    class MyModelAdmin(admin.ModelAdmin):
  888
+        def formfield_for_manytomany(self, db_field, request, **kwargs):
  889
+            if db_field.name == "cars":
  890
+                kwargs["queryset"] = Car.objects.filter(owner=request.user)
  891
+            return super(MyModelAdmin, self).formfield_for_manytomany(db_field, request, **kwargs)
877 892
 
878 893
 .. method:: ModelAdmin.queryset(self, request)
879 894
 

0 notes on commit 6ac4aba

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