Ticket #11882: formfield_for_docs.diff
File formfield_for_docs.diff, 1.7 KB (added by , 14 years ago) |
---|
-
docs/ref/contrib/admin/index.txt
865 865 def formfield_for_foreignkey(self, db_field, request, **kwargs): 866 866 if db_field.name == "car": 867 867 kwargs["queryset"] = Car.objects.filter(owner=request.user) 868 return db_field.formfield(**kwargs)869 868 return super(MyModelAdmin, self).formfield_for_foreignkey(db_field, request, **kwargs) 870 869 871 870 This uses the ``HttpRequest`` instance to filter the ``Car`` foreign key field 872 to only the cars owned by the ``User`` instance.871 to only display the cars owned by the ``User`` instance. 873 872 873 .. method:: ModelAdmin.formfield_for_manytomany(self, db_field, request, **kwargs) 874 875 .. versionadded:: 1.1 876 877 Like the ``formfield_for_foreignkey`` method, the ``formfield_for_manytomany`` 878 method can be overridden to change the default formfield for a many to many 879 field. For example, if an owner can own multiple cars and cars can belong 880 to multiple owners -- a many to many relationship -- you could filter the 881 ``Car`` foreign key field to only display the cars owned by the ``User``:: 882 883 class MyModelAdmin(admin.ModelAdmin): 884 def formfield_for_manytomany(self, db_field, request, **kwargs): 885 if db_field.name == "cars": 886 kwargs["queryset"] = Car.objects.filter(owner=request.user) 887 return super(MyModelAdmin, self).formfield_for_manytomany(db_field, request, **kwargs) 888 874 889 .. method:: ModelAdmin.queryset(self, request) 875 890 876 891 The ``queryset`` method on a ``ModelAdmin`` returns a