Opened 16 years ago
Last modified 8 months ago
#10403 assigned New feature
provide declarative syntax to define FormSets - including ModelFormSet and InlineFormSet
Reported by: | Owned by: | Mitchina | |
---|---|---|---|
Component: | Forms | Version: | dev |
Severity: | Normal | Keywords: | formset modelformset inlineformset |
Cc: | Triage Stage: | Accepted | |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | yes |
Easy pickings: | no | UI/UX: | no |
Description
Provide a declarative mechanism to define modelformsets or inlineformsets.
The attached patch allows definitions like this:
class AuthorForm(forms.ModelForm): class Meta: model = Author class DeclarativeAuthorFormSet(forms.models.ModelFormSet): form = AuthorForm model = Author extra = 3
and
class BookForm(forms.ModelForm): class Meta: model = Book class DeclarativeAuthorBooksFormSet(forms.models.InlineFormSet): model = Book form = BookForm parent = 'author'
An advantage is that the defined form is directly used as the form class in the formset, not as a base class for a new form class (what inlineformset_factory does). This way specific field definitions and other customisations in the form work like they should so you don't need to redefine things in init().
Attachments (2)
Change History (16)
by , 16 years ago
Attachment: | declarativeformsets.diff added |
---|
comment:1 by , 16 years ago
Triage Stage: | Unreviewed → Design decision needed |
---|
comment:2 by , 14 years ago
Severity: | → Normal |
---|---|
Type: | → New feature |
comment:3 by , 14 years ago
Easy pickings: | unset |
---|---|
Keywords: | formset added |
Needs documentation: | set |
Needs tests: | set |
Patch needs improvement: | set |
Summary: | provide declarative syntax to define ModelFormSet and InlineFormSet → provide declarative syntax to define FormSets - including ModelFormSet and InlineFormSet |
UI/UX: | unset |
Closed #16289 as a duplicate - it proposed essentially the same thing as this ticket, but included all FormSets, not just ModelFormSets. Hence I'm widening the scope of this a little. (Also marking as needs tests since the tests in the patch are doctests.)
comment:4 by , 14 years ago
Ok, so I missed this one O:)
I disagree with the implementation in the patch, no MetaClass is needed and just simple additions to the class definitions to make it work, maybe with the exception of InlineFormSet. Will attach a patch later on.
by , 13 years ago
Attachment: | django-10403.diff added |
---|
comment:5 by , 13 years ago
Adding a comment from #16289:
This would be very useful for a nicer implementation of the formset derived cbv #16256.
The BaseInlineFormSet? is the only tricky one with the _get_foreign_key() and the max_num override when fk.unique=True .
I'd also move the defaults (e.g. extra=3) to the BaseFormSet? and make the factories only pass dict keys whose value isn't None to type().
comment:6 by , 12 years ago
Triage Stage: | Design decision needed → Accepted |
---|
The current factory-function implementation of formsets is unnecessarily confusing to new users, and unlike anything else in Django. It can also cause obscure issues with things that should "just work" (see e.g. http://stackoverflow.com/questions/622982/django-passing-custom-form-parameters-to-formset/624013#624013). It's ironic that we provide declarative syntax for things like models and forms where it requires metaclass magic, but in the case of formsets where a declarative syntax is a simple matter of normal Python subclassing, we don't :-)
Haven't reviewed the latest patch in depth, but the broad strokes look correct to me.
comment:10 by , 9 years ago
I don't know that deprecating the factory functions (at least immediately) is a good idea. This could cause a lot of annoying churn for large projects.
comment:11 by , 6 years ago
Owner: | changed from | to
---|---|
Status: | new → assigned |
comment:12 by , 6 years ago
Here is a link to the pull request. This is the first iteration of the patch so may require some critical review.
https://github.com/django/django/pull/11481
comment:14 by , 11 months ago
Owner: | changed from | to
---|
comment:15 by , 10 months ago
I opened a PR (still in draft) to continue the work initiated by Parth Patil on: https://github.com/django/django/pull/17905
comment:16 by , 10 months ago
Needs documentation: | unset |
---|---|
Needs tests: | unset |
Patch needs improvement: | unset |
comment:17 by , 8 months ago
Patch needs improvement: | set |
---|
Left a small review, please update "Patch needs improvement" to False when these changes are applied and I will review more deeply 👍
patch to provide declarative syntax for modelformset and inlineformset