#3987: ModelAdmin should allow for overriding of ForeignKey/ManyToMany options
-----------------------------------------------------------+----------------
Reporter: Baptiste <[EMAIL PROTECTED]> | Owner: nobody
Status: reopened | Milestone:
Component: django.contrib.admin | Version:
newforms-admin
Resolution: | Keywords:
nfa-someday
Stage: Someday/Maybe | Has_patch: 1
Needs_docs: 0 | Needs_tests: 0
Needs_better_patch: 0 |
-----------------------------------------------------------+----------------
Comment (by mwdiers):
There is a serious problem with this patch. It changes the
formfield_for_dbfield method of ModelAdmin, requiring a request object to
passed in as a positional argument. This method is not in the docs, but it
is commonly overridden, and represents a significant change to the API for
the Admin.
There is a way to do what this patch does on 1.0, without any patching:
{{{
class SiteAdmin(ModelAdmin):
def __call__(self, request, url):
#Add in the request object, so that it may be referenced
#later in the formfield_for_dbfield function.
self.request = request
return super(SiteAdmin, self).__call__(request, url)
def get_form(self, request, obj=None):
form = super(SiteAdmin, self).get_form(request, obj)
#print form
return form
def formfield_for_dbfield(self, db_field, **kwargs):
field = super(SiteAdmin, self).formfield_for_dbfield(db_field,
**kwargs) # Get the default field
if db_field.name == 'home_page':
#Add the null object
my_choices = [('', '---------')]
#Grab the current site id from the URL.
my_choices.extend(Page.objects.filter(site=self.request.META['PATH_INFO'].split('/')[-2]).values_list('id','name'))
print my_choices
field.choices = my_choices
return field
}}}
The trick is the override of _ _call_ _, adding the request object as an
attribute of the instance. Once you have this, you can do pretty much
anything you need in the formfield_for_dbfield override. Granted, this is
not very elegant. But it works in 1.0, and our users are already
accustomed to overriding formfield_for_dbfield to make custom form tweaks.
--
Ticket URL: <http://code.djangoproject.com/ticket/3987#comment:31>
Django <http://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/django-updates?hl=en
-~----------~----~----~----~------~----~------~--~---