[英]How to get distinct value from table using ORM methods in odoo
In Odoo, I have a model "Email Rules" , there is one column number_of_days . 在Odoo中,我有一个模型“电子邮件规则” ,其中有一列number_of_days 。
Now what I need to do is to find distinct number_of_days from that model without fire query, I mean I want to use ORM methods not query. 现在,我需要做的是从该模型中查找不同的number_of_days ,而无需进行火灾查询,这意味着我想使用ORM方法而不是查询。
How can I do this ? 我怎样才能做到这一点 ? I don't have much idea about this. 我对此不太了解。
Odoo search_read and search methods to get data from table. Odoo search_read和搜索方法从表中获取数据。
def search_read(self, cr, uid, domain=None, fields=None, offset=0, limit=None, order=None, context=None):
"""
Performs a ``search()`` followed by a ``read()``.
:param cr: database cursor
:param user: current user id
:param domain: Search domain, see ``args`` parameter in ``search()``. Defaults to an empty domain that will match all records.
:param fields: List of fields to read, see ``fields`` parameter in ``read()``. Defaults to all fields.
:param offset: Number of records to skip, see ``offset`` parameter in ``search()``. Defaults to 0.
:param limit: Maximum number of records to return, see ``limit`` parameter in ``search()``. Defaults to no limit.
:param order: Columns to sort result, see ``order`` parameter in ``search()``. Defaults to no sort.
:param context: context arguments.
:return: List of dictionaries containing the asked fields.
:rtype: List of dictionaries.
"""
@api.returns('self')
def search(self, cr, user, args, offset=0, limit=None, order=None, context=None, count=False):
""" search(args[, offset=0][, limit=None][, order=None][, count=False])
Searches for records based on the ``args``
:ref:`search domain <reference/orm/domains>`.
:param args: :ref:`A search domain <reference/orm/domains>`. Use an empty
list to match all records.
:param int offset: number of results to ignore (default: none)
:param int limit: maximum number of records to return (default: all)
:param str order: sort string
:param bool count: if True, only counts and returns the number of matching records (default: False)
:returns: at most ``limit`` records matching the search criteria
:raise AccessError: * if user tries to bypass access rules for read on the requested object.
"""
Now what do I need to do is to find out all the data first then make it unique by pythonic way which is quite repulsive. 现在,我需要做的是先找出所有数据,然后通过pythonic方式使其具有唯一性,这是令人讨厌的。
So it would be great if anyone knows the smart solution for this. 因此,如果有人知道这一点的明智解决方案,那就太好了。
You can use distinct_field_get()
. 您可以使用distinct_field_get()
。
For example if you want to get distinct partners names, you can do it like this: 例如,如果您想获得不同的合作伙伴名称,则可以这样进行:
partners = self.env['res.partner']
values = partners.distinct_field_get(field='name', value='')
return set(values)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.