18.6 Switching to GCBVs with PostGetMixin in Post Views

Instead of removing behavior from DateDetailView, we should be adding behavior to DetailView.

In Chapter 17, we saw that DetailView uses the get_object() method to actually fetch the object from the database. We can therefore create a mixin class to override the method and use multiple inheritance to override this in DetailView and other GCBVs, as shown in Example 18.31.

Example 18.31: Project Code

blog/utils.py in a180f20424

 1   from django.shortcuts import get_object_or_404  2  3   from .models import Post  4  5  6   class PostGetMixin:  7  8       def get_object(self, queryset=None):  9           year = self.kwargs.get('year') 10           month = self ...

Get Django Unleashed now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.