Table Of Contents

Previous topic

ApplicationBase

Next topic

Included Applications

This Page

Model Application

The Basics

Lets assume we have a model called Project, to setup a djpcms application based on a database model, we create a class derived from ModelApplication:

from djpcms.views import appsite

class ProjectApplication(appsite.ModelApplication):
    pass

A dynamic application is registered to the site by invoking:

appsite.site.register('/project/', Project, ProjectApplication)

This application is pretty useless since it has no djpcms.views.appview.AppView associated with it. Infact, such application will throw an djpcms.core.exceptions.ApplicationUrlException because there are no views associated with it. Here is an app which can be used for something meaningful:

from djpcms.views import appsite, appview

class ProjectApplication(appsite.ModelApplication):
    search = appview.SearchView()
    view   = appview.ViewView()

We can now search and view instances of Project.

ModelApplication Class

class djpcms.views.appsite.ModelApplication(baseurl, application_site, editavailable, model=None)

An ApplicationBase class for applications based on a back-end database model. This class implements the basic functionality for a general model User should subclass this for full control on the model application.

model

The model class which own the application

opts

Instance of djpcms.core.models.ModelTypeWrapper. Created from model

list_display

List of object's field to display. If available, the search view will display a sortable table of objects. Default is None.

object_display

Same as list_display attribute at object level. The field list is used to display the object definition. If not available, list_display is used. Default None.

list_per_page

Number of objects per page. Default is 30.

filter_fields

List of model fields which can be used to filter

search_fields

List of model field's names which are searchable. Default None. This attribute is used by djpcms.views.appview.SearchView views and by the auto-complete functionality when searching for model instances.

objectbits(obj)

Get arguments from model instance used to construct url. By default it is the object id. * obj: instance of self.model

It returns dictionary of url bits which are used to uniquely identify a model instance.

get_object(request, **kwargs)

Retrive an instance of self.model from key-values kwargs forming the url. By default it get the 'id' and get the object:

try:
    id = int(kwargs.get('id',None))
    return self.model.objects.get(id = id)
except:
    return None

Re-implement for custom arguments.

object_from_form(form)

Save form and return an instance pof self.model

get_form(djp, form_class, addinputs=True, form_withrequest=None, form_ajax=None, forceform=False, instance=None, **kwargs)

Build a form

getview(code)

Get an application view from the view code.

has_permission(request=None, obj=None)

Return True if the page can be viewed, otherwise False

parentresponse(djp, app)

Retrieve the parent djpcms.views.response.DjpResponse instance

submit(instance, own_view=False)

Generate the submits elements to be added to the model form.

objurl(request, name, obj=None)

Application view name url.

has_view_permission(request=None, obj=None)

Return True if the page can be viewed, otherwise False

basequery(request)

Starting queryset for searching objects in model. This can be re-implemented by subclasses. By default returns all

object_content(djp, obj)

Utility function for getting content out of an instance of a model. This dictionary should be used to render an object within a template. It returns a dictionary.

Create object links

paginate(request, data, prefix, wrapper)

Paginate data

render_object(djp, wrapper=None)

Render an object. This is usually called in the view page of the object

title_object(obj)

Return the title of a object-based view

data_generator(djp, data)

Return a generator for the query. This function can be overritten by derived classes

get_object_view_template(obj, wrapper)

Return the template file which render the object obj. The search looks in:

[<<app_label>>/<<model_name>>.html,
 "djpcms/components/object.html"]
get_item_template(obj, wrapper)
Search item template. Look in
1 - components/<<module_name>>_search_item.html 2 - <<app_label>>/<<module_name>>_search_item.html 3 - djpcms/components/object_search_item.html (fall back)
instancecode(request, obj)

Obtain an unique code for an instance. Can be overritten to include request dictionary.