Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

newforms-admin: Removed formset_for_queryset. BaseModelFormSet's cons…

…tructor now takes a queryset argument instead.

git-svn-id: http://code.djangoproject.com/svn/django/branches/newforms-admin@6839 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 081d0ed8892e6a3e647b9a5a7ab83cb305034fdb 1 parent b15703d
@jkocherhans jkocherhans authored
View
52 django/newforms/models.py
@@ -15,7 +15,7 @@
__all__ = (
'save_instance', 'form_for_model', 'form_for_instance', 'form_for_fields',
- 'formset_for_model', 'formset_for_queryset', 'inline_formset',
+ 'formset_for_model', 'inline_formset',
'ModelChoiceField', 'ModelMultipleChoiceField',
)
@@ -278,15 +278,12 @@ class BaseModelFormSet(BaseFormSet):
model = None
queryset = None
- def __init__(self, data=None, files=None, auto_id='id_%s', prefix=None):
+ def __init__(self, qs, data=None, files=None, auto_id='id_%s', prefix=None):
kwargs = {'data': data, 'files': files, 'auto_id': auto_id, 'prefix': prefix}
- if self.queryset:
- kwargs['initial'] = [initial_data(obj) for obj in self.get_queryset()]
+ self.queryset = qs
+ kwargs['initial'] = [initial_data(obj) for obj in qs]
super(BaseModelFormSet, self).__init__(**kwargs)
- def get_queryset(self):
- return self.queryset._clone()
-
def save_new(self, form, commit=True):
"""Saves and returns a new model instance for the given form."""
return save_instance(form, self.model(), commit=commit)
@@ -306,7 +303,7 @@ def save_existing_objects(self, commit=True):
return []
# Put the objects from self.get_queryset into a dict so they are easy to lookup by pk
existing_objects = {}
- for obj in self.get_queryset():
+ for obj in self.queryset:
existing_objects[obj._get_pk_val()] = obj
saved_instances = []
for form in self.change_forms:
@@ -336,32 +333,6 @@ def add_fields(self, form, index):
form.fields[self._pk_field_name] = IntegerField(required=False, widget=HiddenInput)
super(BaseModelFormSet, self).add_fields(form, index)
-def formset_for_queryset(queryset, form=BaseForm, formfield_callback=lambda f: f.formfield(),
- formset=BaseModelFormSet, extra=1, orderable=False, deletable=False, fields=None):
- """
- Returns a FormSet class for the given QuerySet. This FormSet will contain
- change forms for every instance in the QuerySet as well as the number of
- add forms specified by ``extra``.
-
- Provide ``extra`` to determine the number of add forms to display.
-
- Provide ``deletable`` if you want to allow the formset to delete any
- objects in the given queryset.
-
- Provide ``form`` if you want to use a custom BaseForm subclass.
-
- Provide ``formfield_callback`` if you want to define different logic for
- determining the formfield for a given database field. It's a callable that
- takes a database Field instance and returns a form Field instance.
-
- Provide ``formset`` if you want to use a custom BaseModelFormSet subclass.
- """
- form = form_for_model(queryset.model, form=form, fields=fields, formfield_callback=formfield_callback)
- FormSet = formset_for_form(form, formset, extra, orderable, deletable)
- FormSet.model = queryset.model
- FormSet.queryset = queryset
- return FormSet
-
def formset_for_model(model, form=BaseForm, formfield_callback=lambda f: f.formfield(),
formset=BaseModelFormSet, extra=1, orderable=False, deletable=False, fields=None):
"""
@@ -372,8 +343,10 @@ def formset_for_model(model, form=BaseForm, formfield_callback=lambda f: f.formf
This is essentially the same as ``formset_for_queryset``, but automatically
uses the model's default manager to determine the queryset.
"""
- qs = model._default_manager.all()
- return formset_for_queryset(qs, form, formfield_callback, formset, extra, orderable, deletable, fields)
+ form = form_for_model(model, form=form, fields=fields, formfield_callback=formfield_callback)
+ FormSet = formset_for_form(form, formset, extra, orderable, deletable)
+ FormSet.model = model
+ return FormSet
class InlineFormset(BaseModelFormSet):
"""A formset for child objects related to a parent."""
@@ -382,7 +355,8 @@ def __init__(self, instance, data=None, files=None):
self.instance = instance
# is there a better way to get the object descriptor?
self.rel_name = RelatedObject(self.fk.rel.to, self.model, self.fk).get_accessor_name()
- super(InlineFormset, self).__init__(data, files, prefix=self.rel_name)
+ qs = self.get_queryset()
+ super(InlineFormset, self).__init__(qs, data, files, prefix=self.rel_name)
def get_queryset(self):
"""
@@ -390,10 +364,10 @@ def get_queryset(self):
self.instance
"""
kwargs = {self.fk.name: self.instance}
- return self.queryset.filter(**kwargs)
+ return self.model._default_manager.filter(**kwargs)
def save_new(self, form, commit=True):
- kwargs = {self.fk.get_attname(): self.instance._get_pk_val()}
+ kwargs = {self.fk.get_attname(): self.instance.pk}
new_obj = self.model(**kwargs)
return save_instance(form, new_obj, commit=commit)
View
18 tests/modeltests/model_formsets/models.py
@@ -16,12 +16,12 @@ def __unicode__(self):
__test__ = {'API_TESTS': """
->>> from django.newforms.models import formset_for_queryset, formset_for_model
+>>> from django.newforms.models import formset_for_model
>>> qs = Author.objects.all()
>>> AuthorFormSet = formset_for_model(Author, extra=3)
->>> formset = AuthorFormSet()
+>>> formset = AuthorFormSet(qs)
>>> for form in formset.forms:
... print form.as_p()
<p><label for="id_form-0-name">Name:</label> <input id="id_form-0-name" type="text" name="form-0-name" maxlength="100" /><input type="hidden" name="form-0-id" id="id_form-0-id" /></p>
@@ -35,7 +35,7 @@ def __unicode__(self):
... 'form-2-name': '',
... }
->>> formset = AuthorFormSet(data=data)
+>>> formset = AuthorFormSet(qs, data=data)
>>> formset.is_valid()
True
@@ -54,9 +54,9 @@ def __unicode__(self):
but in that case we'll use it to display them in alphabetical order by name.
>>> qs = Author.objects.order_by('name')
->>> AuthorFormSet = formset_for_queryset(qs, extra=1, deletable=False)
+>>> AuthorFormSet = formset_for_model(Author, extra=1, deletable=False)
->>> formset = AuthorFormSet()
+>>> formset = AuthorFormSet(qs)
>>> for form in formset.forms:
... print form.as_p()
<p><label for="id_form-0-name">Name:</label> <input id="id_form-0-name" type="text" name="form-0-name" value="Arthur Rimbaud" maxlength="100" /><input type="hidden" name="form-0-id" value="2" id="id_form-0-id" /></p>
@@ -73,7 +73,7 @@ def __unicode__(self):
... 'form-2-name': 'Paul Verlaine',
... }
->>> formset = AuthorFormSet(data=data)
+>>> formset = AuthorFormSet(qs, data=data)
>>> formset.is_valid()
True
@@ -91,9 +91,9 @@ def __unicode__(self):
deltetion, make sure we don't save that form.
>>> qs = Author.objects.order_by('name')
->>> AuthorFormSet = formset_for_queryset(qs, extra=1, deletable=True)
+>>> AuthorFormSet = formset_for_model(Author, extra=1, deletable=True)
->>> formset = AuthorFormSet()
+>>> formset = AuthorFormSet(qs)
>>> for form in formset.forms:
... print form.as_p()
<p><label for="id_form-0-name">Name:</label> <input id="id_form-0-name" type="text" name="form-0-name" value="Arthur Rimbaud" maxlength="100" /></p>
@@ -117,7 +117,7 @@ def __unicode__(self):
... 'form-3-DELETE': 'on',
... }
->>> formset = AuthorFormSet(data=data)
+>>> formset = AuthorFormSet(qs, data=data)
>>> formset.is_valid()
True
Please sign in to comment.
Something went wrong with that request. Please try again.