Ticket #11882: 11822.diff

File 11822.diff, 1.1 KB (added by Rob Hudson <treborhudson@…>, 6 years ago)
  • docs/ref/contrib/admin/index.txt

    diff --git a/docs/ref/contrib/admin/index.txt b/docs/ref/contrib/admin/index.txt
    index c1e05ed..ac6520c 100644
    a b return a subset of objects for this foreign key field based on the user:: 
    820820This uses the ``HttpRequest`` instance to filter the ``Car`` foreign key field
    821821to only the cars owned by the ``User`` instance.
    822822
     823Similarly, the ``formfield_for_manytomany`` method can be overridden.
     824For example, if an owner can own multiple cars and cars can belong to
     825multiple owners -- a many to many relationship -- you would need to
     826override the many to many field::
     827
     828    class MyModelAdmin(admin.ModelAdmin):
     829        def formfield_for_manytomany(self, db_field, request, **kwargs):
     830            if db_field.name == "cars":
     831                kwargs["queryset"] = Car.objects.filter(owner=request.user)
     832                return db_field.formfield(**kwargs)
     833            return super(MyModelAdmin, self).formfield_for_manytomany(db_field, request, **kwargs)
     834
    823835Other methods
    824836~~~~~~~~~~~~~
    825837
Back to Top