Help needed in Domain

I have a model called ‘exam_section.exam’. This model has 2 One2Many fields

centers = fields.One2Many('exam.centers', 'exam', 'Centers', states={
        'readonly': ~Eval('state').in_(['draft'])
        }, depends=['state'])

and

employees = fields.One2Many('exam.employees', 'exam', 'Employees', states={
        'readonly': ~Eval('state').in_(['draft'])
        }, depends=['state'])

In ‘exam.employees’ model, I have a Many2One field that links to ‘exam.centers’

center = fields.Many2One(
    'exam.centers',
    'Center',
    domain=[()]     # domain to fetch centers from centers field in 'exam_section.exam'
)

I want to select only those centers which are in the centers field of ‘exam_section.exam’. What should I write in this domain?

You should write a function field that returns all the centers of the exam_section.exam model and use this field in a in clause:

[(‘id’, ‘in’, Eval(‘your_field’)]

Hope it helps

1 Like

I defined a function field as follows

centers = fields.Function(
    fields.One2Many(
        'exam.centers',
        'center_hidden',
        'List of centers'
    ),
    'on_change_with_centers'
)

and wrote ‘on_change_with_centers’ as follows

@fields.depends('exam')
def on_change_with_centers(self, name=None):
    if self.exam and self.exam.centers:
        return self.exam.centers

But on starting the server the server gives out the following error

Traceback (most recent call last):
File "/home/elf9/workspace/apar_tryton/lib/python3.6/site-packages/Werkzeug-0.15.2-py3.6.egg /werkzeug/serving.py", line 302, in run_wsgi
execute(self.server.app)
File "/home/elf9/workspace/apar_tryton/lib/python3.6/site-packages/Werkzeug-0.15.2-py3.6.egg/werkzeug/serving.py", line 290, in execute
application_iter = app(environ, start_response)
File "/home/elf9/workspace/apar_tryton/lib/python3.6/site-packages/trytond-5.0.8-py3.6.egg/trytond/wsgi.py", line 121, in __call__
return self.wsgi_app(environ, start_response)
File "/home/elf9/workspace/apar_tryton/lib/python3.6/site-packages/trytond-5.0.8-py3.6.egg/trytond/wsgi.py", line 127, in __call__
return self.app(environ, start_response)
File "/home/elf9/workspace/apar_tryton/lib/python3.6/site-packages/trytond-5.0.8-py3.6.egg/trytond/wsgi.py", line 100, in wsgi_app
response = cls.response(data, request)
File "/home/elf9/workspace/apar_tryton/lib/python3.6/site-packages/trytond-5.0.8-py3.6.egg/trytond/protocols/jsonrpc.py", line 160, in response
response, cls=JSONEncoder, separators=(',', ':')),
File "/usr/lib/python3.6/json/__init__.py", line 238, in dumps
**kw).encode(obj)
File "/usr/lib/python3.6/json/encoder.py", line 199, in encode
chunks = self.iterencode(o, _one_shot=True)
File "/usr/lib/python3.6/json/encoder.py", line 257, in iterencode
return _iterencode(o, 0)
File "/home/elf9/workspace/apar_tryton/lib/python3.6/site-packages/trytond-5.0.8-py3.6.egg/trytond/protocols/jsonrpc.py", line 61, in default
return marshaller(obj)
File "/usr/lib/python3.6/json/encoder.py", line 180, in default
o.__class__.__name__)
TypeError: Object of type 'exam.centers' is not JSON serializable

The returned value for a One2Many Function must be a list of id not a list of instances.

1 Like

It’s working now
Thanks! :slightly_smiling_face:

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.