django-related is a set of class-based-view mixins that help with common scenarios that involves related objects. The generic class-based views like CreateView or UpdateView only deal with single objects. However, for nested resources, or in cases where we might want to use an existing object if avaialble, these views lack functionality. This is what django-related provides.
Installation can be done using pip:
pip install django-related
There is no need to add related to installed apps. It can be used directly:
from related.views import RedirectOnExistingMixin, CreateWithRelatedMixin ...
This mixin is deprecated. It is currently just an alias for RedirectOnExistingMixin to provide backwards compatibility. It will be removed because of the confusing name.
Only the class name has changed. All property and method names are left intact.
This mixin is used when an attempt to create an object that already exists in the database should result in a redirect. If the object does not exist in the database, it will not do anything.
from related.views import RedirectOnExistingMixin from django.views.gneric import CreateView from models import Foo class MyView(RedirectOnExistingMixin, CreateView): model = Foo existing_redirect_url = '/bar'
With the above view, if we submit a form that contains a pk or slug field, and the Foo object with matching pk or slug field exists, user will be redirected to /bar path, and the model form for Foo will not even be processed. The redirect path can be customized using the options discussed further below.
Note that this mixin _will_ result in extra database queries to determine whether an object exists.
The view can be further customized using the following properties (and matching methods):
- existing_form_class (get_existing_form_class())
- Uses the specified form to process the request rather than request parameters. Default is None (does no use forms).
- Form field that contains data about existence of the object. Note that this field does not need to evaluate to an actual object. Any non-False value will be treated as signifying object's existence. The most common use case is to use a ModelChoiceField or some field that reads the database in advance, and provides a choice of values that are only available when objects exist.
- existing_form_initial (get_existing_form_initial())
- Dictionary of initial values for the existing_form_class form (if form is used).
- The model field that contains the primary key. Default is 'pk'.
- The model field that contains the slug. Default is 'slug'
- The request parameter that represents the primary key. Default is 'pk'.
- The request parameter that represents the slug. Note that if primary key is specified (it is by default), and it is passed in request, slug will not be looked up.
- existing_redirect_url (get_existing_redirect_url())
- Required attribute. The URL that client will be redirected to if the object exists.
- existing_form_name (get_existing_form_name)
- Customizes the name of the context object containing the form.