跳至內容
選單
此問題已被標幟
1 回覆
1201 瀏覽次數

I'm trying to add a SMS permission checkbox to the portal in Odoo.  I have this view working:

<odoo>
    <template id="sms_permission_portal" inherit_id="portal.portal_my_details_fields" name="Add SMS Permission to Portal">
        <xpath expr="//div[8]" position="after">
            <div class="mb-3 sms_permission col-xl-6">
                <label class="col-form-label" for="sms_permission">SMS Permission</label>
                <div class="col-1">
                    <div class="form-check">
                        <input type="checkbox" id="sms_permission" name="sms_permission" class="s_website_form_input form-check-input sms_permission" t-att-checked="'checked' if partner.sms_permission else None" t-att-value="partner.sms_permission" data-fill-with="und>
                    </div>
                </div>
                <div class="s_website_form_field_description small form-text text-muted">I would like to receive SMS text messages.</div>
            </div>
        </xpath>
    </template>
</odoo>

I modified the controller to allow my field:

from odoo.addons.portal.controllers.portal import CustomerPortal

class CustomerPortal(CustomerPortal):

    def _get_optional_fields(self):
        """ This method is there so that we can override the optional fields """
        return ["zipcode", "state_id", "sms_permission", "vat", "company_name"]

I whitelisted the variable to allow use in web forms:

<?xml version="1.0" encoding="utf-8"?>
<odoo>
    <data>

<function model="ir.model.fields" name="formbuilder_whitelist">
    <value>res.partner</value>
    <value eval="[
        'sms_permission',
    ]"/>
</function>
    </data>
</odoo>

Everything seems to work fine.  Except when I uncheck the box it doesn't save.  Checks get saved but unchecks do not.


I understand that's because if the checkbox is unchecked then the browser doesn't send the variable to Odoo.  So Odoo/python doesn't have any way to know that it got modified.


Anyone have a solution for this? 


頭像
捨棄
作者 最佳答案

I ended up solving this by adding this to my controller:

    def on_account_update(self, values, partner):
        if not 'sms_permission' in values.values():
            partner.sms_permission = False

頭像
捨棄
相關帖文 回覆 瀏覽次數 活動
0
12月 23
1422
2
4月 23
4574
2
7月 25
1565
2
5月 25
995
1
11月 24
1281