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'])


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(
    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(
        'List of centers'

and wrote ‘on_change_with_centers’ as follows

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/", line 302, in run_wsgi
File "/home/elf9/workspace/apar_tryton/lib/python3.6/site-packages/Werkzeug-0.15.2-py3.6.egg/werkzeug/", 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/", 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/", line 127, in __call__
return, start_response)
File "/home/elf9/workspace/apar_tryton/lib/python3.6/site-packages/trytond-5.0.8-py3.6.egg/trytond/", 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/", line 160, in response
response, cls=JSONEncoder, separators=(',', ':')),
File "/usr/lib/python3.6/json/", line 238, in dumps
File "/usr/lib/python3.6/json/", line 199, in encode
chunks = self.iterencode(o, _one_shot=True)
File "/usr/lib/python3.6/json/", 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/", line 61, in default
return marshaller(obj)
File "/usr/lib/python3.6/json/", line 180, in default
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.