Ticket #3987: dynamic_related_3987.diff

File dynamic_related_3987.diff, 6.4 KB (added by Jakub Wiśniowski, 17 years ago)

Patch updated to work with version 6426

  • django/newforms/models.py

     
    177177        if value in ('', None):
    178178            return None
    179179        try:
    180             value = self.queryset.model._default_manager.get(pk=value)
     180            value = self.queryset.get(pk=value)
    181181        except self.queryset.model.DoesNotExist:
    182182            raise ValidationError(ugettext(u'Select a valid choice. That choice is not one of the available choices.'))
    183183        return value
     
    200200        final_values = []
    201201        for val in value:
    202202            try:
    203                 obj = self.queryset.model._default_manager.get(pk=val)
     203                obj = self.queryset.get(pk=val)
    204204            except self.queryset.model.DoesNotExist:
    205205                raise ValidationError(ugettext(u'Select a valid choice. %s is not one of the available choices.') % val)
    206206            else:
  • django/contrib/admin/options.py

     
    136136        if self.fieldsets and self.fields:
    137137            raise ImproperlyConfigured('Both fieldsets and fields is specified for %s.' % self.model)
    138138
    139     def formfield_for_dbfield(self, db_field, **kwargs):
     139    def formfield_for_dbfield(self, db_field, request, **kwargs):
    140140        """
    141141        Hook for specifying the form Field instance for a given database Field
    142142        instance.
     
    146146        # For ManyToManyFields with a filter interface, use a special widget.
    147147        if isinstance(db_field, models.ManyToManyField) and db_field.name in (self.filter_vertical + self.filter_horizontal):
    148148            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)
    150152
    151153        # For DateTimeFields, use a special field and widget.
    152154        if isinstance(db_field, models.DateTimeField):
     
    166168
    167169        # For ForeignKey or ManyToManyFields, use a special widget.
    168170        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)
    169175            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)
    171177            else:
    172178                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 = ''
    175181            # Wrap the widget's render() method with a method that adds
    176182            # extra HTML to the end of the rendered output.
    177             formfield = db_field.formfield(**kwargs)
    178183            # Don't wrap raw_id fields. Their add function is in the popup window.
    179184            if not db_field.name in self.raw_id_fields:
    180185                formfield.widget.render = widgets.RelatedFieldWidgetWrapper(formfield.widget.render, db_field.rel, self.admin_site)
    181186            return formfield
    182187
     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       
    183191        # For any other type of field, just call its formfield() method.
    184192        return db_field.formfield(**kwargs)
    185193
     
    335343            fields = flatten_fieldsets(self.declared_fieldsets)
    336344        else:
    337345            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))
    339348
    340349    def form_change(self, request, obj):
    341350        """
     
    345354            fields = flatten_fieldsets(self.declared_fieldsets)
    346355        else:
    347356            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))
    349358
    350359    def save_add(self, request, model, form, formsets, post_url_continue):
    351360        """
     
    711720            fields = flatten_fieldsets(self.declared_fieldsets)
    712721        else:
    713722            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)
    715724
    716725    def formset_change(self, request, obj):
    717726        """Returns an InlineFormSet class for use in admin change views."""
     
    719728            fields = flatten_fieldsets(self.declared_fieldsets)
    720729        else:
    721730            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)
    723732
    724733    def fieldsets_add(self, request):
    725734        if self.declared_fieldsets:
Back to Top