from related.views import GetExistingMixin, CreateWithRelatedMixin
The model field that contains the slug. Default is ``'slug'``
- The request parameter that represents the primary key. Defalt is ``'pk'``.
+ The request parameter that represents the primary key. Defalt is ``'pk'``.
The request parameter that represents the slug. Note that if primary key is
Customizes the name of the context object containing the form.
+This mixin is a generic related object pre-fetching mixin. Before resorting to
+using this mixin, let's first discuss an approach that works well without the
+overhead of ``GetRelatedMixin``, in cases where you are only fetching the
+related object, but doing nothing else. Consider this URL pattern::
+(Yes, there is a whole comment system included in Django, so, sorry for the
+corny example.) In the above case You would normally use a list view for the
+comments. But you might also want to fetch the post object as well. There is no
+need to use ``GetRelatedMixin`` in this particular case, because you can
+combine Django's built-in ``SingleObjectMixin`` with ``ListView`` to achieve
+Another case where you *do not* want to use this mixin is if you can fetch the
+related object using Django's DB API. For example, if you have a ``book``
+object, you can probably get the related author as ``book.author``.
+This mixin is useful only if you need two physically unrelated objects (like
+using two ``SingleObjectMixin`` mixins in a view). If there is no actual
+relationship between the two objects via a foreign key, then you should use
+For users of the previous versions of django-related, it has to be noted that
+this mixin is simply ripped out of what used to be a monolithic
+``CreateWithRelatedMixin_``. It therefore behaves more or less the same as that
+ from related import GetRelatedMixin
+ from django.views.detail import SingleObjectMixin
+ from django.views import FormView
+ from cards.models import Card
+ from cards.forms import MatchCardsForm
+ # view for `/match/(?P<first_card_pk>\d+)/(?<second_card_pk>\d+)/`
+ class ViewAttachment(GetRelatedMixin, SingleObjectMixin, FormView):
+ pk_url_kwarg = 'first_card_pk'
+ related_pk_url_kwarg = 'second_card_pk'
+ form_class = MatchCardsForm
+ template_name = 'attachment.html'
+ 'first_card': self.object.pk,
+ 'second_card': self.related_object.pk,
+ def form_valid(self, form):
+ # Do something with the form, etc
+``GetRelatedMixin`` is currently limited to fetching only one object, just like
+``SingleObjectMixin``, and is therefore not suitable for complex nested
+structures. Again, using Django's DB API would be more reasonable in many of
+The view can be customized using the following attributes (and matching
+ Related model under which the current model is nested. This attribute is
+ If specified, the view will redirect instead of raising
+ ``django.http.Http404`` or returning ``django.http.HttpResponseGone``.
+ If ``related_404_redirect_url`` is used, the ``django.contrib.messages`` is
+ used to display an error message. This attribute is used to customize this
+ message. Default is ``'%s does not exist'`` where ``'%s'`` will evaluate to
+ the ``related_model``'s verbose name.
+ The field on the ``related_model`` that contains the primary key. Defaults
+ The URL parameter that contains the primary key. Defaults to ``'pk'``.
+ The field on the ``related_model`` that contains the sulug field. Defaults
+ The URL parameter that contains the slug field. Defaults to ``'slug'``.
+ Customizes name of the context object that contains the related object.
+ Specifies the object that implements the caching methods. This object is
+ ``django.core.caching.cache`` by default. Any interface that you specify
+ must provide the same methods as the default one.
resource. The main assumption is that higher levels of the path contains a slug
or pk that points to the related model's object.
+As discussed in the ``GetRelatedMixin_`` section, this mixin is based on it, so
+the same customization options are available.
The key difference between normal CreateView, which can be persuaded to give
you the related object using the ``queryset`` attribute and ``get_object``
method, and this mixin, lies in the form processing. This mixin does two things
``django.http.Http404`` or returning ``django.http.HttpResponseGone``.
If ``related_404_redirect_url`` is used, the ``django.contrib.messages`` is
used to display an error message. This attribute is used to customize this
message. Default is ``'%s does not exist'`` where ``'%s'`` will evaluate to
The URL parameter that contains the slug field. Defaults to ``'slug'``.
Customizes name of the context object that contains the related object.