In a recent project I found another area where the generic views fall short: inline formsets.
I have added the ability to use inline formsets in generic views. The new view functions are drop in replacements for Django's with the addition of a new
inlines
argument.Installation
From Source
Download wadofstuff-django-views.
To install it, run the following command inside the unpacked source directory:
python setup.py install
From pypi
If you have the Python
easy_install
utility available, you canalso type the following to download and install in one step:
easy_install wadofstuff-django-views
Or if you're using
pip
:
pip install wadofstuff-django-views
Or if you'd prefer you can simply place the included
wadofstuff
directory somewhere on your Python path, or symlink to it from
somewhere on your Python path; this is useful if you're working from a
Subversion checkout.
Note that this application requires Python 2.4 or later. You can obtain
Python from http://www.python.org/.
Usage
wadofstuff.django.views.create_object(..., inlines=None)
wadofstuff.django.views.update_object(..., inlines=None)
These functions are identical to the Django ones except for the addition of the
inlines
argument. This argument consists of a list of dictionaries that willbe passed as arguments after the
parent_model
argument toinlineformset_factory(parent_model, ...)
.For example, arguments to a generic view might typically look like:
crud_dict = {would translate to calls to
'model':Author
'inlines':[{
'model':Book,
'extra':2,
'form':BookForm,
},{
'model':Article,
}],
# ... other generic view arguments
}
inlineformset_factory()
like:
inlineformset_factory(Author, model=Book, extra=2, form=BookForm)
and
inlineformset_factory(Author, model=Article)
The view function will create a formset for each inline model and add them to the template context. In the example above the context variables would be named
book_formset
and article_formset
.Update: A quick change to allow the views to be imported from
wadofstuff.django.views
. Bumped version to 1.0.1.
3 comments:
Wow, this is great. I really needed an implementation like this and it's perfect. The only thing I'd recommend updating would be to store the formsets in a dictionary/list so that you can write a single template that will iterate over any of these formsets and create the markup. It'd be a little more "generic".
Thanks for the code!
This is *ACE* stuff!!!
It's just AMAZING! thanks a lot!
Post a Comment