Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[IMP] Order groups by selection declaration order in read_group #75856

Closed

Commits on Sep 6, 2021

  1. [IMP] Order groups by selection declaration order in read_group

    Before this commit, calling read_group on a model and grouping by a
    selection field would return the groups in alphabetical order, meaning
    that if your selection field's options were declared as:
    
    ('z', 'Z'), ('a', 'A')
    
    read_group would return first group 'a' and then group 'z', instead of
    groups 'z' and then 'a' which some might expect.
    
    With this commit, it is now possible to set a fields.Selection
    group_expand attribute to `True` when declaring it, which will use a
    default group_expand implementation specific to Selection fields, this
    means that when grouping by a Selection field with `group_expand=True`
    it will always return the groups in the definition order of the
    selection options.
    
    We achieve this by leveraging the group_expand field attribute which was
    designed for changing the groups returned by read_group.
    
    Since this attribute was thought only to be implemented on a
    Model-by-Model basis and here we need to use it as a generic function
    for all Selection fields (explicit group_expand declarations have higher
    precedence), the generic method has been implemented inside
    fields.Selection and takes an extra records parameter which holds a
    reference to the recordset/model on which read_group was called. This
    extra parameter only applies to field implementations of group_expand
    and in this case is what allows this specific feature to work with
    dynamic Selection fields (function as options).
    
    A side-effect of this implementation is that a read_group call that
    groups by a Selection field with `group_expand=True` that uses the
    default group_expand will always return all possible groups, even
    empty ones.
    
    Task-ID 2635052
    Adrian Torres authored and rco-odoo committed Sep 6, 2021
    Copy the full SHA
    637ef29 View commit details
    Browse the repository at this point in the history