Search code examples
odooxml-rpcodoo-9

How to change selection fields with filters search in odoo? [Odoo 9]


I have created a state on models

state = fields.Selection([
        ('new', 'New'),
        ('draft', 'Draft'),
        ('approved', 'Approved')
        ],default='new')

I make menuitems that are different for user and admin. But in the admin groups, i get an error after adding filters search.

<record id="view_admin_filter" model="ir.ui.view">
    <field name="name">Admin</field>
    <field name="model">mymodels</field>
    <field name="arch" type="xml">
        <search string="Admin">
             <filter string="Draft" name="state" domain="[('state','=','draft')]"/>
        </search>
    </field>
</record>

and

<record id="open_module_tree_admin" model="ir.actions.act_window">
    <field name="name">Admin</field>
    <field name="res_model">mymodels</field>
    <field name="view_type">form</field>
    <field name="view_mode">tree,form</field>
        <field name="context">{
            "search_default_state": 1,
            "default_state": 1}
        </field>
        <field name="domain">[]</field>
        <field name="view_ids" eval="[(5, 0, 0),
            (0, 0, {'view_mode': 'tree', 'view_id': ref('view_admin_tree')}),
            (0, 0, {'view_mode': 'form', 'view_id': ref('view_admin_form')})]"/>
        <field name="search_view_id" ref="view_admin_filter"/>
        <field name="help" type="html">
        <p class="oe_view_nocontent_create">
            Click to create.
        </p>
    </field>
</record>

I found an error like this

ValueError: Wrong value for mail.mail.state: 1

I have tried to replace it like this

"search_default_state": 'draft',
"default_state": 'draft'}

but it still doesn't work and getting error

ValueError: Wrong value for mail.mail.state: u'draft'

how should I fix it?


Solution

  • From what you said you are setting a default value for state some where in your code with a value that is not in selection.

    like for example when you did this:

       "default_state": 1 
    

    This will cause this error to happen because I'm sure that 1 is not valid value instead doing this is correct.

      "default_state": 'draft'
    

    But only if your selection has this value 'draft'.

    One thing you should know in XML removing the code of the context from the action definition will not remove it from the data base (you will keep having the same problem).

    To fix this problem remove this default values from your code then do it again step by step and make sure you upgrade the moduel and restart the server.

        <record id="open_module_tree_admin" model="ir.actions.act_window">
            <field name="name">Admin</field>
            <field name="res_model">mymodels</field>
            <field name="view_type">form</field>
            <field name="view_mode">tree,form</field>
            <field name="context">{}</field>   <!-- this will update the context to {} -->
            .....
            ....
    

    and check your python code for default value or onchange event make sure your not setting the state field to a wrong value.

      fields.Selection(.... default='draft')
    

    Hope this helps you

    And for your filter don't give them names like your fields names

      <filter string="Draft" name="draft_state" domain="[('state','=','draft')]"/>
    

    This way you can apply this filter in the context of the action like this:*

      {'search_default_draft_state': 1}
    

    I think it's safer.