URLPatterns autogeneration mechanisms: Router¶
One of the key architectural concepts of CRUDLFA+ is the ability to tie a group of view with a model class to autogenerate urlpatterns. This chapter reviews the different mechanisms in place and how they are overridable.
Source is located in the
we’ll describe here.
CRUDLFA+ router for Django 2.0.
Note that you can also use non-database backed models, by inheriting from models.Model and setting their Meta.managed attribute to False. Then, you can use CRUDLFA+ views and routers.
Router(model=None, registry=None, views=None, **attributes)¶
Base router for CRUDLFA+ Route.
Optional model class for this Router and all its views.
Return True to allowed a access to a view.
Called by the default view.allowed() implementation.
If you override the view.allowed() method, then it’s up to you to decide if you want to call this method or not.
Returns True if user.is_staff by default.
Generate views for this router, core of the automation in CRUDLFA+.
This method considers each view in given args or self.views and returns a list of usable views.
Each arg may be a view class or a dict of attributes with a _cls key for the actual view class.
It will copy the view class and bind the router on it in the list this returns.
For example, this would cause two view classes to be returned, if self.model is
CreateViewwill be used as parent to create
DetailViewwill be used to create
ArtistDetailView, also setting the attribute
Router(Artist).generate_views([ CreateView, dict(_cls=DetailView, extra_stuff='bar'), ListView.factory(paginate_by=12), ])
Generate app name for this Router views.
get_fields_for_user(user, perms, obj=None)¶
Return the list of fields for a user.
Return allowed view objects which have
For each view class in self.views which have
menusattribute, instanciate the view class with
requestand kwargs, call
Return the list of view instances for which
Generate namespace for this Router views.
Return the list of objects for a given set of perms.
Return Field name of model for reversing url.
This will return model ` slug ` field if available or ` pk ` field.
Return Model name for urlpath.
Generate URL patterns for this Router views.
Register to self.registry.
Also, adds the get_absolute_url() method to the model class if it has None, to return the reversed url for this instance to the view of this Router with the
Set get_absolute_url in your model class to disable this feature. Until then, welcome in 2018.
Also, register this router as default router for its model class in the RouterRegistry.