Ticket #11882: 11882.diff

File 11882.diff, 1.4 KB (added by Tim Graham, 15 years ago)

small edits to existing patch

  • docs/ref/contrib/admin/index.txt

     
    818818            return super(MyModelAdmin, self).formfield_for_foreignkey(db_field, request, **kwargs)
    819819
    820820This uses the ``HttpRequest`` instance to filter the ``Car`` foreign key field
    821 to only the cars owned by the ``User`` instance.
     821to only display the cars owned by the ``User`` instance.
    822822
     823.. method:: ModelAdmin.formfield_for_manytomany(self, db_field, request, **kwargs)
     824
     825.. versionadded:: 1.1
     826
     827Like the ``formfield_for_foreignkey`` method, the ``formfield_for_manytomany``
     828method can be overridden to change the default formfield for a many to many
     829field. For example, if an owner can own multiple cars and cars can belong
     830to 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
    823840Other methods
    824841~~~~~~~~~~~~~
    825842
Back to Top