The base type of EntityAdmin, is ObjectAdmin, which specifies most of the class attributes that can be used to customize the interface.
The ObjectAdmin class describes the interface that will be used to interact with objects of a certain class. The behaviour of this class and the resulting interface can be tuned by specifying specific class attributes:
The name used in the GUI
The name used in the GUI for things like window titles and such can be specified using the verbose_name attribute.
A human-readable name for the object, singular
verbose_name = _('movie')
If this isn’t given, the class name will be used
A human-readable name for the object, plural
verbose_name_plural = _('movies')
If this isn’t given, Camelot will use verbose_name + “s”
Fields displayed
a list with the fields that should be displayed in a table view
the number of columns on the left of the tableview that should be frozen (don’t dissapear when the user uses the horizontal scroll bar), defaults to zero
An integer number specifying the height of a row in the table view, expressed as the number of lines of text it should be able to display. Defaults to 1.
a list with the fields that should be displayed in a form view, defaults to the same fields as those specified in list_display
class Admin(EntityAdmin):
form_display = ['title', 'rating', 'cover']
instead of telling which fields to display. It is also possible to define the form itself
from camelot.view.forms import Form, TabForm, WidgetOnlyForm, HBoxForm
class Admin(EntityAdmin):
form_display = TabForm([
('Movie', Form([
HBoxForm([['title', 'rating'], WidgetOnlyForm('cover')]),
'short_description',
'releasedate',
'director',
'script',
'genre',
'description', 'tags'], scrollbars=True)),
('Cast', WidgetOnlyForm('cast'))
])
Behaviour
Specifies when the data should be send from the view to the model and flushed to the database. The default mode is ‘on_change’, meaning that every change in the view will be send immediately to the database. Other possibilities are :
- ‘on_leave’ : the data will be send from the view to the model when the view
is closed, eg. : the form is closed.
Indicates if the deletion of an object should be confirmed by the user, defaults to ‘on_request’, indicating object should be deleted when the user hits the trash button. Other possibilities are :
- ‘on_confirm’ : the user will be asked for confirmation before the delete takes place.
a tuple indicating the size of a form view, defaults to (700,500)
Actions to be accessible by pushbuttons on the side of a form, a list of tuples (button_label, action_function) where action_function takes as its single argument, a method that returns the the object that was displayed by the form when the button was pressed:
class Admin(EntityAdmin):
form_actions = [('Foo', lamda o_getter:print 'foo')]
Field attributes
A dictionary specifying for each field of the model some additional attributes on how they should be displayed. All of these attributes are propagated to the constructor of the delegate of this field:
class Movie(Entity):
title = Field(Unicode(50))
class Admin(EntityAdmin):
list_display = ['title']
field_attributes = dict(title=dict(editable=False))
The Field Attributes documentation describes the various keys that can be used in the field attributes class attribute of an ObjectAdmin or EntityAdmin.
Window state
Set this attribute to ‘maximized’ or ‘minimized’ for respective behaviour. These are the only two defined at the moment. Please use the constants defined in camelot.core.constants (MINIMIZE and MAXIMIZE). Note that this attr needs to be set at the form, highest in the form hierarchy to work. Setting this on embedded forms will not influence the window state. Example:
class Movie(Entity):
title = Field(Unicode(50))
class Admin(EntityAdmin):
from camelot.core import constants
list_display = ['title']
form_state = constants.MAXIMIZED
field_attributes = dict(title=dict(editable=False))
Varia
The QAbstractItemModel class to be used to display collections of this object, defaults to a CollectionProxy
The QWidget class to be used when a table view is needed
Other Admin classes can inherit ObjectAdmin if they want to provide additional functionallity, like introspection to set default field_attributes.
Note
While EntityAdmin can only be used for classes that are mapped by Sqlalchemy, ObjectAdmin can be used for plain old python objects as well.
A typical use case of an ObjectAdmin, is to display properties of Entity classes that return lists:
class B(object)
...
class Admin(ObjectAdmin):
...
class A(Entity)
...
@property
def list_of_b(self):
return [b1, b2, b3]
class Admin(EntityAdmin):
form_display = ['list_of_b']
field_attributes = {'list_of_b': {'delegate':delegates.One2ManyDelegate,
'target':B}}