Ticket #9076: inline_queryset.diff

File inline_queryset.diff, 1.7 KB (added by Bob Thomas, 16 years ago)

Use the same QuerySet for all forms in the FormSet

  • django/forms/models.py

     
    447447class BaseInlineFormSet(BaseModelFormSet):
    448448    """A formset for child objects related to a parent."""
    449449    def __init__(self, data=None, files=None, instance=None,
    450                  save_as_new=False, prefix=None):
     450                 save_as_new=False, prefix=None, queryset=None):
    451451        from django.db.models.fields.related import RelatedObject
    452452        self.instance = instance
    453453        self.save_as_new = save_as_new
    454454        # is there a better way to get the object descriptor?
    455455        self.rel_name = RelatedObject(self.fk.rel.to, self.model, self.fk).get_accessor_name()
    456         super(BaseInlineFormSet, self).__init__(data, files, prefix=prefix or self.rel_name)
     456        if queryset is None:
     457            kwargs = {self.fk.name: self.instance}
     458            queryset = self.model._default_manager.filter(**kwargs)
     459        super(BaseInlineFormSet, self).__init__(data, files, prefix=prefix or self.rel_name,
     460            queryset=queryset)
    457461
    458462    def _construct_forms(self):
    459463        if self.save_as_new:
     
    469473            form.data[form.add_prefix(self._pk_field.name)] = None
    470474        return form
    471475
    472     def get_queryset(self):
    473         """
    474         Returns this FormSet's queryset, but restricted to children of
    475         self.instance
    476         """
    477         kwargs = {self.fk.name: self.instance}
    478         return self.model._default_manager.filter(**kwargs)
    479 
    480476    def save_new(self, form, commit=True):
    481477        kwargs = {self.fk.get_attname(): self.instance.pk}
    482478        new_obj = self.model(**kwargs)
Back to Top