Snippet List
By combining the CreateView and UpdateView, you can significantly reduce repetition when processing complex forms (for example, with multiple inline formsets), by only writing the get_context_data and form_valid functions once.
This class can be used just like a normal CreateView or UpdateView.
Note that if you're trying to use it as an UpdateView but it cannot find the requested object, it will behave as a CreateView, rather than showing a 404 page.
- generic-view
- class-based-generic-view
- UpdateView
- CreateView
Code for a RelatedMixin I whipped up, useful in instances where you wish to expose details of a single object, including a related group of owned objects, in the same view. Works well with Django's generic DetailView and UpdateView, or any subclass of SingleObjectMixin.
It's a little cleaner than overriding get_context_data differently for every model you want to expose, uses `only('id')` on querysets it doesn't need anything but relational data from, and makes pulling ownership out of distantly related objects much easier.
Supports simple nested hierarchies of models, ie:
* View a company and all people belonging to it Detail(Company < People)
* Edit a company and all computers belonging to its members Update(Company < People < Computers).
Tested with non-generic One-To-Many and reverse object_sets only.
Just provide an OrderedDict called `related_chain` in your DetailRelatedView that describes the progression of querysets to follow, in the format:
model=Foo,
relation_chain=OrderedDict([
('foreign_key_from_foo_to_bar',Bar.objects.all()),
('foreign_key_from_baz_to_bar',Baz.objects.all())
])
It also takes two optional attributes:
context_list_name="baz_list"
which provides an alias for the final related `object_list` (default=`related_list`), and
keep_intermediaries=True
which, if providing a list deeper than one relation, also passes any intermediary related lists into the context, named after the connecting foreign key, like `bar_list` (default=False).
- mixin
- model-filtering
- class-based-generic-view
If you did tried to use `reverse` function to set `success_url` in class based generic views and you have got an exception, this helper function may help.
Put this snipped in some file, for example utils.py and import this function.
- reverse
- class-based-generic-view
- success_url
3 snippets posted so far.