Ticket #3987: dynamic_choice_9014.diff
File dynamic_choice_9014.diff, 4.4 KB (added by , 16 years ago) |
---|
-
django/contrib/admin/options.py
39 39 radio_fields = {} 40 40 prepopulated_fields = {} 41 41 42 def formfield_for_dbfield(self, db_field, **kwargs):42 def formfield_for_dbfield(self, db_field, request, **kwargs): 43 43 """ 44 44 Hook for specifying the form Field instance for a given database Field 45 45 instance. … … 112 112 113 113 # For ForeignKey or ManyToManyFields, use a special widget. 114 114 if isinstance(db_field, (models.ForeignKey, models.ManyToManyField)): 115 if hasattr(self, 'dynamic_%s_choices' % db_field.name): 116 formfield = db_field.formfield(queryset = getattr(self, 'dynamic_%s_choices' % db_field.name)(request, db_field.rel.to), **kwargs) 117 else: 118 formfield = db_field.formfield(**kwargs) 115 119 if isinstance(db_field, models.ForeignKey) and db_field.name in self.raw_id_fields: 116 kwargs['widget']= widgets.ForeignKeyRawIdWidget(db_field.rel)120 formfield.widget = widgets.ForeignKeyRawIdWidget(db_field.rel) 117 121 elif isinstance(db_field, models.ForeignKey) and db_field.name in self.radio_fields: 118 kwargs['widget']= widgets.AdminRadioSelect(attrs={122 formfield.widget = widgets.AdminRadioSelect(attrs={ 119 123 'class': get_ul_class(self.radio_fields[db_field.name]), 120 124 }) 121 kwargs['empty_label']= db_field.blank and _('None') or None125 formfield.empty_label = db_field.blank and _('None') or None 122 126 else: 123 127 if isinstance(db_field, models.ManyToManyField): 124 128 # If it uses an intermediary model, don't show field in admin. 125 129 if db_field.rel.through is not None: 126 130 return None 127 131 elif db_field.name in self.raw_id_fields: 128 kwargs['widget']= widgets.ManyToManyRawIdWidget(db_field.rel)129 kwargs['help_text']= ''132 formfield.widget = widgets.ManyToManyRawIdWidget(db_field.rel) 133 formfield.help_text = '' 130 134 elif db_field.name in (list(self.filter_vertical) + list(self.filter_horizontal)): 131 kwargs['widget']= widgets.FilteredSelectMultiple(db_field.verbose_name, (db_field.name in self.filter_vertical))135 formfield.widget = widgets.FilteredSelectMultiple(db_field.verbose_name, (db_field.name in self.filter_vertical)) 132 136 # Wrap the widget's render() method with a method that adds 133 137 # extra HTML to the end of the rendered output. 134 formfield = db_field.formfield(**kwargs)135 138 # Don't wrap raw_id fields. Their add function is in the popup window. 136 139 if not db_field.name in self.raw_id_fields: 137 140 # formfield can be None if it came from a OneToOneField with … … 140 143 formfield.widget = widgets.RelatedFieldWidgetWrapper(formfield.widget, db_field.rel, self.admin_site) 141 144 return formfield 142 145 146 if isinstance(db_field, (models.OneToOneField)) and hasattr(self, 'dynamic_%s_choices' % db_field.name): 147 return db_field.formfield(queryset = getattr(self, 'dynamic_%s_choices' % db_field.name)(request, db_field.rel.to), **kwargs) 148 143 149 # For any other type of field, just call its formfield() method. 144 150 return db_field.formfield(**kwargs) 145 151 … … 271 277 "form": self.form, 272 278 "fields": fields, 273 279 "exclude": exclude + kwargs.get("exclude", []), 274 "formfield_callback": self.formfield_for_dbfield,280 "formfield_callback": lambda f, **kwargs: self.formfield_for_dbfield(f, request, **kwargs), 275 281 } 276 282 defaults.update(kwargs) 277 283 return modelform_factory(self.model, **defaults) … … 787 793 "fk_name": self.fk_name, 788 794 "fields": fields, 789 795 "exclude": exclude + kwargs.get("exclude", []), 790 "formfield_callback": self.formfield_for_dbfield,796 "formfield_callback": lambda f, **kwargs: self.formfield_for_dbfield(f, request, **kwargs), 791 797 "extra": self.extra, 792 798 "max_num": self.max_num, 793 799 }