Ticket #18597: formset-optimisation.diff

File formset-optimisation.diff, 1.8 KB (added by Simon Charette, 12 years ago)

ticket-18597.0.diff

  • django/forms/models.py

    diff --git a/django/forms/models.py b/django/forms/models.py
    index 26f8691..fdf786d 100644
    a b class BaseInlineFormSet(BaseModelFormSet):  
    690690        self.save_as_new = save_as_new
    691691        # is there a better way to get the object descriptor?
    692692        self.rel_name = RelatedObject(self.fk.rel.to, self.model, self.fk).get_accessor_name()
     693        filter_queryset = True
    693694        if queryset is None:
    694             queryset = self.model._default_manager
    695         qs = queryset.filter(**{self.fk.name: self.instance})
     695            if (self.instance.pk and
     696                self.fk.rel.multiple and not self.fk.rel.is_hidden()):
     697                try:
     698                    queryset = getattr(instance, self.rel_name).get_query_set()
     699                except AttributeError:
     700                    # It's possible to get here in model inheritance scenarios
     701                    # where `self.model` is a subclass `self.fk.model` and
     702                    # thus `self.fk.rel.to` has no reverse descriptor for
     703                    # `self.model`. If it's not the the case we let the
     704                    # `AttributeError` propagate.
     705                    if not issubclass(self.model, self.fk.model):
     706                        raise
     707                else:
     708                    filter_queryset = False # It's already filtered
     709            if queryset is None:
     710                queryset = self.model._default_manager
     711        if filter_queryset:
     712            queryset = queryset.filter(**{self.fk.name: self.instance})
    696713        super(BaseInlineFormSet, self).__init__(data, files, prefix=prefix,
    697                                                 queryset=qs, **kwargs)
     714                                                queryset=queryset, **kwargs)
    698715
    699716    def initial_form_count(self):
    700717        if self.save_as_new:
Back to Top