0

I have a model in a custom plugin that I'm developing with a field that I want to restrict to a particular domain. My model looks something like:

from openerp.osv import fields, osv

class MyModel(osv.Model):
    _name = 'mymodel'
    _columns = {
        'ui_code_version': fields.many2one(
            comodel_name='robots.software.versions', 
            string='UI Code Version', 
            domain="[('project', '=', 'ui')]"
        )
    }

When directly editing an instance of my model, it seems like the domain is properly restricting my choices to ui project versions, which is great.

However, if I set up the Mass Editing module so that I can edit the ui versions of multiple instances of mymodel, the domain filter is not applied and I can erroneously select software versions from other projects.

Poking around in the database, it looks like the domain isn't even being stored there.

MyOdooDatabase> SELECT name, domain FROM ir_model_fields \
                WHERE model = 'mymodel' AND name = 'ui_code_version'
+-----------------------+----------+
| name                  |   domain |
|-----------------------+----------|
| ui_code_version       |   <null> |
+-----------------------+----------+

Similarly, the domain isn't in the Settings->Technical->Database Structure->Fields entry for this field:

Screenshot of Field Settings

Q: How can I get the Mass Editing module to obey my fields domain so that I can only select ui project versions? Does that domain need to be stored in the database? If so, how?


Edit: As requested by Bhavesh Odedra, below are the definitions for robots.software.versions and robots.software.projects:

class RobotsSoftwareVersions(osv.Model):
    _name = 'robots.software.versions'
    _description = 'Software Version'
    _columns = {
        'name': fields.char('Name'),
        'project': fields.many2one('robots.software.projects', 'Project'),
        'version': fields.char('Version', help='The git tag or hash (e.g. 3.2.1)')
    }

class RobotsSoftwareProjects(osv.Model):
    _name = 'robots.software.projects'
    _columns = {
        'name': fields.char('Name'),
        'upstream': fields.char('Upstream'),
    }
rcv
  • 6,078
  • 9
  • 43
  • 63

1 Answers1

0

Domain should be

domain="[('project', '=', 'ui')]"

You may reference https://odedrabhavesh.blogspot.com/2015/09/domain-in-odoo.html for future development.

Updated

  • Add project_name field in robots.software.versions object and make it related with project like

    'project_name': fields.related('project', 'name', type='char', store=True)

  • Now update your domain like

    domain=[('project_name', '=', 'ui')]

Bhavesh Odedra
  • 10,990
  • 12
  • 33
  • 58
  • I had a typo in my stackoverflow post. The domain was correctly defined already in my actual code. See above for the edit. Nice catch though! – rcv Oct 12 '18 at 15:54
  • `project` field is declared `many2one` so It will store integer ID in database. U will enter project name as `ui` correct ? – Bhavesh Odedra Oct 12 '18 at 16:14
  • Yup, that's correct. I wasn't actually sure if the `domain` specification would work, but it does when I'm editing a single field (see here: https://imgur.com/a/MPFatg9). – rcv Oct 12 '18 at 18:08
  • I modified the `robots.software.versions` to include the `project_name` field and updated the domain accordingly. The result is the same: software versions are filtered when editing a single robot, but are not filtered when using the Mass Edit module. Also, the domain is not listed in the entry in the `ir_model_fields` database table. – rcv Oct 12 '18 at 20:26