如何将创建名称的元素限制为已经具有同名元素的模型? Odoo 12

How to restrict a Element with a name getting created to the Model that already have an element with the same name? Odoo 12

a = kw.get('a')           #text getting from the user input
models = request.env['htpmodel']
for model in models:
   if str(a) != str(model.name):
        h = model.create({
            'name': a,
        })

如果用户输入(a)的str不在模型名称中则需要创建否则不需要创建重复元素

我建议首先使用您提供的名称搜索记录:

a = kw.get("a")

models = request.env["htpmodel"]
# search to see if a record with that name already exists
record = models.search([("name", "=", a)], limit=1)

if not record:
    # the record doesn't exist
    h = model.create({
        "name": a,
    })

查询模型比遍历结果更好。

如果您需要任何说明,请告诉我,

谢谢,