Odoo 13:如何编写好的过滤器以便将数据发送到odoo网站页面?

问题描述 投票:1回答:1

我被要求做一些过滤器,然后再将数据传递到网站。我有与many2many字段链接的四(4)个模型。让我添加四个模型的图像。enter image description here

为了打印model.a,我们需要检查它是否链接了model.b,然后检查是否有model.c链接到了model.b,最后检查是否有model.d链接到了模型毕竟。结果与此图像enter image description here相同为此,我编写了以下代码:

@http.route(['/agenda'], auth="public", website=True)
def agenda(self):
    months = DATES_SELECT
    # all dictionary used in the implementation
    model_c_dict = {}
    model_b_dict = {}
    model_a_dict = {}
    model_a_key = []
    # filter the model.d according to certain condition
    # should I set registrations_left field as store=True for performance when using .search()
    model_d_ids = request.env['model.d'].search([('date_start', '>', dt.now().date()), ('state', '=', 'opened')], order="date_start").filtered(lambda k: k.registrations_left != 0)
    for session in model_d_ids:
        course_id = session.course_id_many[:1]
        if not course_id.state == 'validated':
            continue
        model_c_dict.setdefault(course_id.id, {'object': course_id, 'sessions': []})
        model_c_dict[course_id.id]['sessions'].append(session)
    for k, v in model_c_dict.items():
        category_id = v['object'].category_ids[:1]
        if not category_id:
            continue
        model_b_dict.setdefault(category_id.id, {'object': category_id, 'course': {}})
        model_b_dict[category_id.id]['course'].setdefault(k, v)
    for k, v in model_b_dict.items():
        catalogue_id = v['object'].catalogue_ids[:1]
        if not catalogue_id:
            continue
        model_a_dict.setdefault(catalogue_id.id, {'object': catalogue_id, 'category':  {}})
        model_a_dict[catalogue_id.id]['category'].setdefault(k, v)
        if catalogue_id.id in model_a_dict:
            model_a_key.append(catalogue_id)
    # sort the model_a with model_a.sequence as key
    model_a_key = sorted(list(set(model_a_key)), key=lambda k: k.sequence)

    # pack key
    dict_key = {'model_a_key': model_a_key}

    values = {
        'months': months,
        'categs': model_a_dict,
        'dict_key': dict_key,
    }

    return request.render('website_custom.agenda', values)

它按预期工作,但我不知道它是否存在性能问题,如果编码不正确,...所以我问你的意见。PS:我没有设计模型及其关系。

python odoo
1个回答
1
投票

我喜欢使用切片技术来避免index out of range error,这对于检查记录是否已连接非常有用。一直到过滤函数k.course_id_many[:1].category_ids[:1].catalogue_ids[:1]中的A(目录模型),但我更喜欢在域中这样做:

@http.route(['/agenda'], auth="public", website=True)
def agenda(self):
    courses_dict = {}
    category_dict = {}
    catalogue_dict = {}
    # extract all record of Model D connected all the way up to A model
    sessions = request.env['model.d'].search([('date_start', '>', dt.now().date()),
                                              ('state', '=', 'opened'),
                                              # this will make sure that the record retrieved will be connected to catalogue model (A)
                                              ('course_id_many.category_ids.catalogue_ids', '!=', False)], order="date_start") \
        .filtered(lambda k: k.registrations_left != 0)

    for session in sessions:
        # if you want to treat olny the first record you can add the slice on the many2many [:1]
        # but I think you will skip the rest of the record in the many2many field
        # and if this what you want the loop are not needed at all just do `course = session.course_id_many[0]`
        # and do the same for all loops. because you don't have to check if the record are connected we all ready did that in search method
        course = session.course_id_many[0]
        if not course.state == 'validated': continue  # skip validated courses

        # add course to dict, and add the session to it's list of sessions
        course_obj = courses_dict.setdefault(course.id, {'object': course, 'sessions': []})
        course_obj['sessions'].append(session)

        category = course.category_ids[0]
        # store category, and add course to it's list of courses
        category_obj = category_dict.setdefault(category.id, {'object': category, 'course': {}})
        category_obj = category_dict[category.id]['course'][course.id] = course_obj

        catalogue = category.catalogue_ids[0]
        # sotre catalog, and add category to it's categories list
        catalogue_dict.setdefault(catalogue.id, {'object': catalogue, 'category': {}})['category'][category.id] = category_obj

    # sort catalogue
    catalogue_keys = sorted(catalogue_dict.keys(), key=lambda k: catalogue_dict[k]['object'].sequence)

    values = {
        'months': DATES_SELECT,
        'categs': catalogue_dict,
        'dict_key': catalogue_keys,
    }

    return request.render('website_custom.agenda', values)

希望我能尽最大努力检查语法错误,它应该可以工作。

© www.soinside.com 2019 - 2024. All rights reserved.