在你的领域尝试下面的域功能,我相信你会得到你需要的输出。
student_id = fields.Many2one('op.student',domain=lambda self: self.get_not_existing_student_id(), string='Student')
@api.model
def get_not_existing_student_id(self):
self.env.cr.execute("select id from op_student where id not in (select student_id from model_name)")
datas = self.env.cr.fetchall()
student_ids = []
for record in datas:
student_ids.append(record[0])
return [('id','in',student_ids)]
另一种方式:
student_id = fields.Many2one('op.student',string='Student')
把它放在一个视图,如:
<field name="student_id" context="{'find_existed':1}" options="{'no_create':1}"/>
然后继承方法name_get()在这样op.student模型。
class op_student(models.Model):
_inherit ="op.student"
@api.multi
def name_get(self):
if self._context.get("find_existed",False):
self.env.cr.execute("select id from op_student where id not in (select student_id from model_name)")
datas = self.env.cr.fetchall()
student_ids = []
for record in datas:
student_ids.append(record[0])
for student in self:
if student in student_ids:
res.append((student.id,student.name))
else:
res=super(op_student,self).name_get()
return res
我认为您需要先删除该模型中的所有记录。 ,因为它只适用于模型中没有现有数据的情况。 –
谢谢,现在工作。但是同样的学生在该下拉列表中仍然可见。正在工作的事情是,形式不是为同一个学生储蓄。我希望学生不应该在下拉列表中显示。请建议一种方法。 –
您可以使用该字段student_id上的动态域来仅显示任何记录中不存在的记录。 –