mirror of
https://github.com/django/django.git
synced 2025-07-04 09:49:12 +00:00
[1.2.X] Fixed #15208 - Document ModelAdmin.formfield_for_choice_field; thanks julien.
Backport of r15399 from trunk. git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.2.X@15400 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
01fce67c17
commit
f6606da3e8
@ -887,6 +887,25 @@ to multiple owners -- a many to many relationship -- you could filter the
|
|||||||
kwargs["queryset"] = Car.objects.filter(owner=request.user)
|
kwargs["queryset"] = Car.objects.filter(owner=request.user)
|
||||||
return super(MyModelAdmin, self).formfield_for_manytomany(db_field, request, **kwargs)
|
return super(MyModelAdmin, self).formfield_for_manytomany(db_field, request, **kwargs)
|
||||||
|
|
||||||
|
.. method:: ModelAdmin.formfield_for_choice_field(self, db_field, request, **kwargs)
|
||||||
|
|
||||||
|
Like the ``formfield_for_foreignkey`` and ``formfield_for_manytomany``
|
||||||
|
methods, the ``formfield_for_choice_field`` method can be overridden to
|
||||||
|
change the default formfield for a field that has declared choices. For
|
||||||
|
example, if the choices available to a superuser should be different than
|
||||||
|
those available to regular staff, you could proceed as follows::
|
||||||
|
|
||||||
|
class MyModelAdmin(admin.ModelAdmin):
|
||||||
|
def formfield_for_choice_field(self, db_field, request, **kwargs):
|
||||||
|
if db_field.name == "status":
|
||||||
|
kwargs['choices'] = (
|
||||||
|
('accepted', 'Accepted'),
|
||||||
|
('denied', 'Denied'),
|
||||||
|
)
|
||||||
|
if request.user.is_superuser:
|
||||||
|
kwargs['choices'] += (('ready', 'Ready for deployment'),)
|
||||||
|
return super(MyModelAdmin, self).formfield_for_choice_field(db_field, request, **kwargs)
|
||||||
|
|
||||||
.. method:: ModelAdmin.has_add_permission(self, request)
|
.. method:: ModelAdmin.has_add_permission(self, request)
|
||||||
|
|
||||||
Should return ``True`` if adding an object is permitted, ``False``
|
Should return ``True`` if adding an object is permitted, ``False``
|
||||||
|
Loading…
x
Reference in New Issue
Block a user