Kanban View is probably the most flexible view in Odoo. It can be used for many different purposes. One of the most common ones is splitting items to distinct groups (represented by a row of columns) and allowing users T o Drag items between those groups. For example the
hr_reqruitment
module lets users to manage applications the this.
It ' s trivial and well documented how to group objects in Kanban. Just Add default_group_by
attribute to the <kanban>
element:
<kanban default_group_by="company_id">
including empty groups
There is however a potential problem. Columns representing groups without any items won't be included. This means users won ' t being able to move items to those absent groups, which are probably not what we intended.
Oddo has a answer for this ready-an optional model attribute called _group_by_full
. It should is a dictionary, mapping field names (of the fields you use for grouping) to methods returning information about All available groups to those fields.
ClassStore(Models.Model):@api. ModelDefCompany_groups(Self,Present_ids,Domain,**Kwargs):Companies=Self.Env[' Res.company ']search ([]) . Name_get () return companies_name = ' store ' _group_by_full = { ' company_id ' : company_groups< Span class= "P", } name = fields.< span class= "n" >char () company_id = fields many2many ( ' Res.company ' )
The code above ensures if displaying store
objects grouped company_id
by, all available companies would be represented (a nd not only those already has stores).
Methods listed in _group_by_full
need to return a, element tuple:
First ELEMENT:A list of the element tuples, representing individual groups. Every tuple in the list need to include the particular group's value (in US example:id of a particular company) and a US ER friendly name for the group (with our Example:company ' s name). That's why we can use name_get
the method, since it returns a list of (object id, object name)
tuples.
Second element:a Dictionary mapping groups ' values to a Boolean value, indicating whether the group should is folded in K Anban view. Not including a group in this dictionary have the same meaning as mapping it to False
.
For example this version of the company_groups
method would make group representing a company with the ID 1
folded in Kanban view:
@api. ModelDefcompany_groups (selfpresent_ Idsdomain**kwargs ): companies = self. Env[ ' Res.company ' ]. Search ([]) . Name_get () folded = {1 : true return companies folded
Grouping by fields other than
many2one
There seem to being problem in Odoo 8.0 preventing use of with fields other _group_by_full
than many2one
. I got around the issue extending the _read_group_fill_results
method. Example of grouping by the state
field:
ClassStore(Models.Model):_name=' Store 'States=[(' Good ',' Good Store '),(' Bad ',' Bad Store '),(' Ugly ',' Ugly Store '),]# states that should being folded in Kanban view# used by the ' state_groups ' methodFolded_states=[' Ugly ',]@api. ModelDefState_groups(Self,Present_ids,Domain,**Kwargs):Folded={Key:(KeyInchSelf.Folded_states)ForKey,_InchSelf.States}# need to copy self. States list before returning it,# because Odoo modifies the list it gets,# emptying it in the process. Bad Odoo!ReturnSelf.States[:],Folded_group_by_full={' State ':State_groups}Name=Fields.Char()State=Fields.Selection(States,Default=' Good ')Def_read_group_fill_results(Self,Cr,Uid,Domain,GroupBy,Remaining_groupbys,Aggregated_fields,Count_field,Read_group_result,Read_group_order=None,Context=None):"""The method seems to support grouping using M2OWhile we want to group by a simple status field.Hence The code below-it replaces simple status valuesWith (value, name) tuples."""IfGroupBy==' State ':States_dict=Dict(Self.States)ForResultInchRead_group_result:State=Result[' State ']Result[' State ']=(State,States_dictget (statereturn super (storeself ) . _read_group_fill_results (cruid domaingroupbyremaining_groupbysaggregated_fields, count_fieldread_group_result Read_group_ordercontext )
Displaying A full list of groups in Odoo ' s Kanban view