mirror of
https://github.com/django/django.git
synced 2025-08-01 17:42:56 +00:00
Cleaned up and refactored ModelAdmin.formfield_for_dbfield
:
* The new method uses an admin configuration option (`formfield_overrides`); this makes custom admin widgets especially easy. * Refactored what was left of `formfield_for_dbfield` into a handful of smaller methods so that it's easier to hook in and return custom fields where needed. * These `formfield_for_*` methods now pass around `request` so that you can easily modify fields based on request (as in #3987). Fixes #8306, #3987, #9148. Thanks to James Bennet for the original patch; Alex Gaynor and Brian Rosner also contributed. git-svn-id: http://code.djangoproject.com/svn/django/trunk@9760 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
d579e716fe
commit
f212b24b64
8 changed files with 369 additions and 83 deletions
22
tests/regressiontests/admin_widgets/widgetadmin.py
Normal file
22
tests/regressiontests/admin_widgets/widgetadmin.py
Normal file
|
@ -0,0 +1,22 @@
|
|||
"""
|
||||
|
||||
"""
|
||||
from django.contrib import admin
|
||||
|
||||
import models
|
||||
|
||||
class WidgetAdmin(admin.AdminSite):
|
||||
pass
|
||||
|
||||
|
||||
class CarTireAdmin(admin.ModelAdmin):
|
||||
def formfield_for_foreignkey(self, db_field, request, **kwargs):
|
||||
if db_field.name == "car":
|
||||
kwargs["queryset"] = models.Car.objects.filter(owner=request.user)
|
||||
return db_field.formfield(**kwargs)
|
||||
return super(CarTireAdmin, self).formfield_for_foreignkey(db_field, request, **kwargs)
|
||||
|
||||
site = WidgetAdmin()
|
||||
|
||||
site.register(models.Car)
|
||||
site.register(models.CarTire, CarTireAdmin)
|
Loading…
Add table
Add a link
Reference in a new issue