Stage 0 — Register a model¶
Goal¶
Add a Django model and expose default CRUD routes with a
ModelController in djmvc.py.
Model¶
from django.db import models
class Item(models.Model):
name = models.CharField(max_length=100)
def __str__(self):
return self.name
Controller and registration¶
Create yourapp/djmvc.py, add yourapp to INSTALLED_APPS, define your
controller, and append it to djmvc.site:
import djmvc
from .models import Item
class ItemController(djmvc.ModelController):
model = Item
icon = 'inbox'
routes = djmvc.ModelController.routes + [
djmvc.generic.ListView.clone(site_search=True),
]
djmvc.site.routes.append(ItemController)
Before build(), site.routes is the declaration list;
append() adds your controller there. Build then autodiscovers every
djmvc.py module (importing it runs the append) and gives you list, create,
detail, update, delete, and bulk-delete views with Bulma templates.
After migrate, log in (see Install djmvc) and visit
http://localhost:8000/item/. URL names look like site:item:list,
site:item:create.
Default list view for the Item model — table, sidebar navigation, and
create action.¶
List and detail use Django’s view permission (view_item). Create,
update, and delete use add, change, and delete.
Site search¶
With djmvc_dal_topbar installed (see install-site-search), the navbar
includes a site-wide search autocomplete. List views are not included by
default — opt in with site_search=True on the list route:
routes = djmvc.ModelController.routes + [
djmvc.generic.ListView.clone(site_search=True),
]
The cloned list view replaces the default list route (same codename).
Search uses each list’s search_fields
(CharField and TextField columns by default). Models without site_search —
for example audit logs from djmvc_history — stay out of the top bar even
when they have searchable columns.
djmvc_auth opts in User and Group list views the same way.
A success toast appears after creating a row.¶
With djmvc_history in INSTALLED_APPS (see Install djmvc), every
ModelController also gets a history view at
http://localhost:8000/item/<pk>/history/ with no extra code in your
djmvc.py.
Select rows on the list to open the built-in bulk-delete action:
Built-in DeleteObjectsView — registered on
every ModelController with no extra code.¶