Ticket #3987: dynamic_related_3987.diff
File dynamic_related_3987.diff, 6.4 KB (added by , 17 years ago) |
---|
-
django/newforms/models.py
177 177 if value in ('', None): 178 178 return None 179 179 try: 180 value = self.queryset. model._default_manager.get(pk=value)180 value = self.queryset.get(pk=value) 181 181 except self.queryset.model.DoesNotExist: 182 182 raise ValidationError(ugettext(u'Select a valid choice. That choice is not one of the available choices.')) 183 183 return value … … 200 200 final_values = [] 201 201 for val in value: 202 202 try: 203 obj = self.queryset. model._default_manager.get(pk=val)203 obj = self.queryset.get(pk=val) 204 204 except self.queryset.model.DoesNotExist: 205 205 raise ValidationError(ugettext(u'Select a valid choice. %s is not one of the available choices.') % val) 206 206 else: -
django/contrib/admin/options.py
136 136 if self.fieldsets and self.fields: 137 137 raise ImproperlyConfigured('Both fieldsets and fields is specified for %s.' % self.model) 138 138 139 def formfield_for_dbfield(self, db_field, **kwargs):139 def formfield_for_dbfield(self, db_field, request, **kwargs): 140 140 """ 141 141 Hook for specifying the form Field instance for a given database Field 142 142 instance. … … 146 146 # For ManyToManyFields with a filter interface, use a special widget. 147 147 if isinstance(db_field, models.ManyToManyField) and db_field.name in (self.filter_vertical + self.filter_horizontal): 148 148 kwargs['widget'] = widgets.FilteredSelectMultiple(db_field.verbose_name, (db_field.name in self.filter_vertical)) 149 return db_field.formfield(**kwargs) 149 if hasattr(self, 'dynamic_%s_choices' % db_field.name): 150 formfield = db_field.formfield(queryset = getattr(self, 'dynamic_%s_choices' % db_field.name)(request, db_field.rel.to), **kwargs) 151 #return db_field.formfield(**kwargs) 150 152 151 153 # For DateTimeFields, use a special field and widget. 152 154 if isinstance(db_field, models.DateTimeField): … … 166 168 167 169 # For ForeignKey or ManyToManyFields, use a special widget. 168 170 if isinstance(db_field, (models.ForeignKey, models.ManyToManyField)): 171 if hasattr(self, 'dynamic_%s_choices' % db_field.name): 172 formfield = db_field.formfield(queryset = getattr(self, 'dynamic_%s_choices' % db_field.name)(request, db_field.rel.to), **kwargs) 173 else: 174 formfield = db_field.formfield(**kwargs) 169 175 if isinstance(db_field, models.ForeignKey) and db_field.name in self.raw_id_fields: 170 kwargs['widget']= widgets.ForeignKeyRawIdWidget(db_field.rel)176 formfield.widget = widgets.ForeignKeyRawIdWidget(db_field.rel) 171 177 else: 172 178 if isinstance(db_field, models.ManyToManyField) and db_field.name in self.raw_id_fields: 173 kwargs['widget']= widgets.ManyToManyRawIdWidget(db_field.rel)174 kwargs['help_text']= ''179 formfield.widget = widgets.ManyToManyRawIdWidget(db_field.rel) 180 formfield.help_text = '' 175 181 # Wrap the widget's render() method with a method that adds 176 182 # extra HTML to the end of the rendered output. 177 formfield = db_field.formfield(**kwargs)178 183 # Don't wrap raw_id fields. Their add function is in the popup window. 179 184 if not db_field.name in self.raw_id_fields: 180 185 formfield.widget.render = widgets.RelatedFieldWidgetWrapper(formfield.widget.render, db_field.rel, self.admin_site) 181 186 return formfield 182 187 188 if isinstance(db_field, (models.OneToOneField)) and hasattr(self, 'dynamic_%s_choices' % db_field.name): 189 return db_field.formfield(queryset = getattr(self, 'dynamic_%s_choices' % db_field.name)(request, db_field.rel.to), **kwargs) 190 183 191 # For any other type of field, just call its formfield() method. 184 192 return db_field.formfield(**kwargs) 185 193 … … 335 343 fields = flatten_fieldsets(self.declared_fieldsets) 336 344 else: 337 345 fields = None 338 return forms.form_for_model(self.model, fields=fields, formfield_callback=self.formfield_for_dbfield) 346 return forms.form_for_model(self.model, fields=fields, 347 formfield_callback=lambda f, **kwargs: self.formfield_for_dbfield(f, request, **kwargs)) 339 348 340 349 def form_change(self, request, obj): 341 350 """ … … 345 354 fields = flatten_fieldsets(self.declared_fieldsets) 346 355 else: 347 356 fields = None 348 return forms.form_for_instance(obj, fields=fields, formfield_callback= self.formfield_for_dbfield)357 return forms.form_for_instance(obj, fields=fields, formfield_callback=lambda f, **kwargs: self.formfield_for_dbfield(f, request, **kwargs)) 349 358 350 359 def save_add(self, request, model, form, formsets, post_url_continue): 351 360 """ … … 711 720 fields = flatten_fieldsets(self.declared_fieldsets) 712 721 else: 713 722 fields = None 714 return forms.inline_formset(self.parent_model, self.model, fk_name=self.fk_name, fields=fields, formfield_callback= self.formfield_for_dbfield, extra=self.extra)723 return forms.inline_formset(self.parent_model, self.model, fk_name=self.fk_name, fields=fields, formfield_callback=lambda f, **kwargs: self.formfield_for_dbfield(f, request, **kwargs), extra=self.extra) 715 724 716 725 def formset_change(self, request, obj): 717 726 """Returns an InlineFormSet class for use in admin change views.""" … … 719 728 fields = flatten_fieldsets(self.declared_fieldsets) 720 729 else: 721 730 fields = None 722 return forms.inline_formset(self.parent_model, self.model, fk_name=self.fk_name, fields=fields, formfield_callback= self.formfield_for_dbfield, extra=self.extra)731 return forms.inline_formset(self.parent_model, self.model, fk_name=self.fk_name, fields=fields, formfield_callback=lambda f, **kwargs: self.formfield_for_dbfield(f, request, **kwargs), extra=self.extra) 723 732 724 733 def fieldsets_add(self, request): 725 734 if self.declared_fieldsets: