Stage 3 — Override default views¶
Goal¶
Replace default views with clone() and add an
extra update view that edits a single field.
Model¶
from django.db import models
class Article(models.Model):
title = models.CharField(max_length=200)
body = models.TextField(blank=True)
category = models.CharField(max_length=50, blank=True)
def __str__(self):
return self.title
Controller and registration¶
import djmvc
from .models import Article
class CategoryUpdateView(djmvc.generic.UpdateView):
"""Update a single field — shows in the object action menu."""
fields = ["category"]
title = "Change category"
icon = "tag"
color = "info"
class ArticleController(djmvc.ModelController):
model = Article
icon = 'newspaper'
routes = djmvc.ModelController.routes + [
djmvc.generic.ListView.clone(
table_fields=["title", "category"],
filter_fields=["category"],
paginate_by=5,
),
CategoryUpdateView,
]
djmvc.site.routes.append(ArticleController)
ModelController.routes + [...] starts from the default route list and
registers your entries afterward. Routes with the same codename replace the
default — here the cloned ListView overrides list,
and CategoryUpdateView adds a new object-menu action.
The cloned list view sets table columns, filter fields, and page size.
CategoryUpdateView is a second update route
(http://localhost:8000/article/<pk>/categoryupdate/) that only exposes the
category field and appears in the object action menu alongside the full
update view.
Try it¶
Visit http://localhost:8000/article/.
Cloned list view with custom columns, filter fields, and page size.¶
Open an article’s detail page — the object menu shows both Change Article and Change category:
Default update plus CategoryUpdateView in the object menu.¶