Ticket #3987: diff-dynamic.3.diff
File diff-dynamic.3.diff, 8.6 KB (added by , 17 years ago) |
---|
-
db/models/fields/related.py
544 544 setattr(cls, related.get_accessor_name(), ForeignRelatedObjectsDescriptor(related)) 545 545 546 546 def formfield(self, **kwargs): 547 defaults = {'form_class': forms.ModelChoiceField, 'queryset': self.rel.to._default_manager.all()} 547 defaults = {'form_class': forms.ModelChoiceField} 548 #To prevent an useless query 549 if not 'queryset' in kwargs: 550 defaults['queryset'] = self.rel.to._default_manager.all() 548 551 defaults.update(kwargs) 549 552 return super(ForeignKey, self).formfield(**defaults) 550 553 … … 605 608 cls._meta.one_to_one_field = self 606 609 607 610 def formfield(self, **kwargs): 608 defaults = {'form_class': forms.ModelChoiceField, 'queryset': self.rel.to._default_manager.all()} 611 defaults = {'form_class': forms.ModelChoiceField} 612 if not 'queryset' in kwargs: 613 defaults['queryset'] = self.rel.to._default_manager.all() 609 614 defaults.update(kwargs) 610 615 return super(OneToOneField, self).formfield(**defaults) 611 616 … … 711 716 return getattr(obj, self.attname).all() 712 717 713 718 def formfield(self, **kwargs): 714 defaults = {'form_class': forms.ModelMultipleChoiceField, 'queryset': self.rel.to._default_manager.all()} 719 defaults = {'form_class': forms.ModelMultipleChoiceField} 720 if not 'queryset' in kwargs: 721 defaults['queryset'] = self.rel.to._default_manager.all() 715 722 defaults.update(kwargs) 716 723 # If initial is passed in, it's a list of related objects, but the 717 724 # MultipleChoiceField takes a list of IDs. -
newforms/models.py
170 170 if value in ('', None): 171 171 return None 172 172 try: 173 value = self.queryset. model._default_manager.get(pk=value)173 value = self.queryset.get(pk=value) 174 174 except self.queryset.model.DoesNotExist: 175 175 raise ValidationError(ugettext(u'Select a valid choice. That choice is not one of the available choices.')) 176 176 return value … … 193 193 final_values = [] 194 194 for val in value: 195 195 try: 196 obj = self.queryset. model._default_manager.get(pk=val)196 obj = self.queryset.get(pk=val) 197 197 except self.queryset.model.DoesNotExist: 198 198 raise ValidationError(ugettext(u'Select a valid choice. %s is not one of the available choices.') % val) 199 199 else: -
contrib/admin/options.py
103 103 raw_id_fields = () 104 104 fields = None 105 105 106 def formfield_for_dbfield(self, db_field, **kwargs):106 def formfield_for_dbfield(self, db_field, request, **kwargs): 107 107 """ 108 108 Hook for specifying the form Field instance for a given database Field 109 109 instance. … … 113 113 # For ManyToManyFields with a filter interface, use a special widget. 114 114 if isinstance(db_field, models.ManyToManyField) and db_field.name in (self.filter_vertical + self.filter_horizontal): 115 115 kwargs['widget'] = widgets.FilteredSelectMultiple(db_field.verbose_name, (db_field.name in self.filter_vertical)) 116 return db_field.formfield(**kwargs) 116 if hasattr(self, 'dynamic_%s_choices' % db_field.name): 117 formfield = db_field.formfield(queryset = getattr(self, 'dynamic_%s_choices' % db_field.name)(request, db_field.rel.to), **kwargs) 117 118 118 119 # For DateTimeFields, use a special field and widget. 119 120 if isinstance(db_field, models.DateTimeField): … … 133 134 134 135 # For ForeignKey or ManyToManyFields, use a special widget. 135 136 if isinstance(db_field, (models.ForeignKey, models.ManyToManyField)): 137 if hasattr(self, 'dynamic_%s_choices' % db_field.name): 138 formfield = db_field.formfield(queryset = getattr(self, 'dynamic_%s_choices' % db_field.name)(request, db_field.rel.to), **kwargs) 139 else: 140 formfield = db_field.formfield(**kwargs) 136 141 if isinstance(db_field, models.ForeignKey) and db_field.name in self.raw_id_fields: 137 kwargs['widget'] = widgets.ForeignKeyRawIdWidget(db_field.rel) 138 return db_field.formfield(**kwargs) 142 formfield.widget = widgets.ForeignKeyRawIdWidget(db_field.rel) 139 143 else: 140 144 # Wrap the widget's render() method with a method that adds 141 145 # extra HTML to the end of the rendered output. 142 formfield = db_field.formfield(**kwargs)143 146 formfield.widget.render = widgets.RelatedFieldWidgetWrapper(formfield.widget.render, db_field.rel) 144 return formfield 147 return formfield 148 149 if isinstance(db_field, (models.OneToOneField)) and hasattr(self, 'dynamic_%s_choices' % db_field.name): 150 return db_field.formfield(queryset = getattr(self, 'dynamic_%s_choices' % db_field.name)(request, db_field.rel.to), **kwargs) 145 151 146 152 # For any other type of field, just call its formfield() method. 147 153 return db_field.formfield(**kwargs) … … 414 420 # Object list will give 'Permission Denied', so go back to admin home 415 421 post_url = '../../../' 416 422 417 ModelForm = forms.form_for_model(model, formfield_callback= self.formfield_for_dbfield)423 ModelForm = forms.form_for_model(model, formfield_callback=lambda f, **kwargs: self.formfield_for_dbfield(f, request, **kwargs)) 418 424 419 425 inline_formsets = [] 420 426 if request.POST: … … 422 428 if opts.has_field_type(models.FileField): 423 429 new_data.update(request.FILES) 424 430 form = ModelForm(new_data) 425 for FormSet in self.get_inline_formsets( ):431 for FormSet in self.get_inline_formsets(request): 426 432 inline_formset = FormSet(data=new_data) 427 433 inline_formsets.append(inline_formset) 428 434 if all_valid(inline_formsets) and form.is_valid(): 429 435 return self.save_add(request, model, form, inline_formsets, '../%s/') 430 436 else: 431 437 form = ModelForm(initial=request.GET) 432 for FormSet in self.get_inline_formsets( ):438 for FormSet in self.get_inline_formsets(request): 433 439 inline_formset = FormSet() 434 440 inline_formsets.append(inline_formset) 435 441 … … 467 473 if request.POST and request.POST.has_key("_saveasnew"): 468 474 return self.add_view(request, form_url='../../add/') 469 475 470 ModelForm = forms.form_for_instance(obj, formfield_callback= self.formfield_for_dbfield)476 ModelForm = forms.form_for_instance(obj, formfield_callback=lambda f, **kwargs: self.formfield_for_dbfield(f, request, **kwargs)) 471 477 472 478 inline_formsets = [] 473 479 if request.POST: … … 475 481 if opts.has_field_type(models.FileField): 476 482 new_data.update(request.FILES) 477 483 form = ModelForm(new_data) 478 for FormSet in self.get_inline_formsets( ):484 for FormSet in self.get_inline_formsets(request): 479 485 inline_formset = FormSet(obj, new_data) 480 486 inline_formsets.append(inline_formset) 481 487 … … 483 489 return self.save_change(request, model, form, inline_formsets) 484 490 else: 485 491 form = ModelForm() 486 for FormSet in self.get_inline_formsets( ):492 for FormSet in self.get_inline_formsets(request): 487 493 inline_formset = FormSet(obj) 488 494 inline_formsets.append(inline_formset) 489 495 … … 610 616 ] 611 617 return render_to_response(template_list, extra_context, context_instance=template.RequestContext(request)) 612 618 613 def get_inline_formsets(self ):619 def get_inline_formsets(self, request): 614 620 inline_formset_classes = [] 615 621 for opts in self.inlines: 616 inline = inline_formset(self.model, opts.model, formfield_callback= opts.formfield_for_dbfield, fields=opts.fields, extra=opts.extra)622 inline = inline_formset(self.model, opts.model, formfield_callback=lambda f, **kwargs: opts.formfield_for_dbfield(f, request, **kwargs), fields=opts.fields, extra=opts.extra) 617 623 inline_formset_classes.append(inline) 618 624 return inline_formset_classes 619 625