| | 823 | .. method:: ModelAdmin.formfield_for_manytomany(self, db_field, request, **kwargs) |
| | 824 | |
| | 825 | .. versionadded:: 1.1 |
| | 826 | |
| | 827 | Like the ``formfield_for_foreignkey`` method, the ``formfield_for_manytomany`` |
| | 828 | method can be overridden to change the default formfield for a many to many |
| | 829 | field. For example, if an owner can own multiple cars and cars can belong |
| | 830 | to multiple owners -- a many to many relationship -- you could filter the |
| | 831 | ``Car`` foreign key field to only display the cars owned by the ``User``:: |
| | 832 | |
| | 833 | class MyModelAdmin(admin.ModelAdmin): |
| | 834 | def formfield_for_manytomany(self, db_field, request, **kwargs): |
| | 835 | if db_field.name == "cars": |
| | 836 | kwargs["queryset"] = Car.objects.filter(owner=request.user) |
| | 837 | return db_field.formfield(**kwargs) |
| | 838 | return super(MyModelAdmin, self).formfield_for_manytomany(db_field, request, **kwargs) |
| | 839 | |