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

I heve been trying to extend the portal user information by adding some fields. At the odoo backend it works propery but at the frontend user dashboard when you want to save the data, it raises an error that says "Unknown field 'company_cac_number,company_registration_type,industry,business_description,company_website,company_email,company_phone,company_logo'"

Here are my codes:
1. __manifest__ 

{
'name': 'Partner Company Extension',
'version': '1.1',
'category': 'Contacts',
'summary': 'Adds company details fields to res.partner',
'depends': ['base', 'portal'],
'data': [
'views/partner_view.xml',
'views/portal_templates.xml',
],
'installable': True,
'application': False,
}

2. /controllers/portal .py

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


class CustomerPortalExtended(CustomerPortal):

def details_form_fields(self):
"""Extend portal details form fields with custom fields"""
fields = super().details_form_fields()
fields.extend([
'company_cac_number',
'company_registration_type',
'industry',
'business_description',
'company_website',
'company_email',
'company_phone',
'company_logo',
])
return fields

@http.route(['/my/account'], type='http', auth="user", website=True, methods=['POST'])
def details_form_submit(self, **kwargs):
# allow default processing first
response = super(CustomerPortalExtended, self).details_form_submit(**kwargs)
# then explicitly write our custom fields
partner = request.env.user.partner_id.sudo()
values = {k: v for k, v in kwargs.items() if k in self.details_form_fields()}
if values:
partner.write(values)
return response

3. models/partner .py

from odoo import models, fields

class ResPartner(models.Model):
_inherit = 'res.partner'

company_cac_number = fields.Char(string="Company CAC Number")
company_registration_type = fields.Selection([
('business_name', 'Business Name'),
('limited_liability', 'Limited Liability'),
('limited_by_guarantee', 'Limited by Guarantee'),
('incorporated_trustee', 'Incorporated Trustee')
], string="Company Type")
industry = fields.Char(string="Industry")
business_description = fields.Text(string="Business Description")
company_website = fields.Char(string="Company Website")
company_email = fields.Char(string="Company Email")
company_phone = fields.Char(string="Company Phone")
company_logo = fields.Binary(string="Company Logo")

4. /views/partner_view .xml

 

<?xml version="1.0" encoding="UTF-8"?>
<odoo>
<record id="view_partner_form_inherit_company" model="ir.ui.view">
<field name="name">res.partner.form.company.extension</field>
<field name="model">res.partner</field>
<field name="inherit_id" ref="base.view_partner_form"/>
<field name="arch" type="xml">
<sheet position="inside">
<group string="Company Information">
<field name="company_cac_number"/>
<field name="company_registration_type"/>
<field name="industry"/>
<field name="business_description"/>
<field name="company_website"/>
<field name="company_email"/>
<field name="company_phone"/>
</group>
</sheet>
</field>
</record>
</odoo>

5. /views/portal_template .xml

<?xml version="1.0" encoding="utf-8"?>
<odoo>
<template id="portal_my_details_inherit" inherit_id="portal.portal_my_details">
<xpath expr="//div[@class='row o_portal_details']//div[@class='row']" position="inside">
<div class="form-group">
<label for="company_cac_number">CAC Number</label>
<input type="text" name="company_cac_number"
t-att-value="partner.company_cac_number or ''"
class="form-control"/>
</div>

<div class="form-group">
<label for="company_registration_type">Registration Type</label>
<select name="company_registration_type" class="form-select">
<option value="ltd" t-att-selected="partner.company_registration_type == 'ltd'">Limited</option>
<option value="enterprise" t-att-selected="partner.company_registration_type == 'enterprise'">Enterprise</option>
<option value="ngo" t-att-selected="partner.company_registration_type == 'ngo'">NGO</option>
</select>
</div>

<div class="form-group">
<label for="industry">Industry</label>
<input type="text" name="industry" t-att-value="partner.industry or ''" class="form-control"/>
</div>

<div class="form-group">
<label for="business_description">Business Description</label>
<textarea name="business_description" class="form-control"><t t-esc="partner.business_description"/></textarea>
</div>

<div class="form-group">
<label for="company_website">Website</label>
<input type="url" name="company_website" t-att-value="partner.company_website or ''" class="form-control"/>
</div>

<div class="form-group">
<label for="company_email">Email</label>
<input type="email" name="company_email" t-att-value="partner.company_email or ''" class="form-control"/>
</div>

<div class="form-group">
<label for="company_phone">Phone</label>
<input type="text" name="company_phone" t-att-value="partner.company_phone or ''" class="form-control"/>
</div>

<div class="form-group">
<label for="company_logo">Company Logo</label>
<input type="file" name="company_logo" class="form-control"/>
</div>
</xpath>
</template>
</odoo>


Thanks in anticipation!




頭像
捨棄

I have deleted my post @Chijoke, as I misunderstood your question. I though it was related to Partner Form.

最佳答案

Hi,



Try the following,


1-Align the selection values


Update your portal template options to match your model’s selection keys:


<select name="company_registration_type" class="form-select">

    <option value="business_name" t-att-selected="partner.company_registration_type == 'business_name'">Business Name</option>

    <option value="limited_liability" t-att-selected="partner.company_registration_type == 'limited_liability'">Limited Liability</option>

    <option value="limited_by_guarantee" t-att-selected="partner.company_registration_type == 'limited_by_guarantee'">Limited by Guarantee</option>

    <option value="incorporated_trustee" t-att-selected="partner.company_registration_type == 'incorporated_trustee'">Incorporated Trustee</option>

</select>



2- Handle file upload for company_logo



In your controller:


import base64

@http.route(['/my/account'], type='http', auth="user", website=True, methods=['POST'])

def details_form_submit(self, **kwargs):

    response = super(CustomerPortalExtended, self).details_form_submit(**kwargs)

    partner = request.env.user.partner_id.sudo()

    values = {k: v for k, v in kwargs.items() if k in self.details_form_fields()}


    # Handle file upload

    file = request.httprequest.files.get('company_logo')

    if file:

        values['company_logo'] = base64.b64encode(file.read())


    if values:

        partner.write(values)

    return response


Hope it helps

頭像
捨棄
最佳答案

I don't know where you've got the idea of

def details_form_fields(self):
    super().details_form_fields()
    ...

from. Certainly not a thing since Odoo 16 - haven't checked earlier versions though. You're actually looking for _get_optional_fields() - or _get_mandatory_fields() if necessary, i.e.:

class CustomerPortalExtended(CustomerPortal):

    def _get_optional_fields(self):
        """Extend portal details form fields with custom fields"""
        fields = super()._get_optional_fields()
        fields.append(
            'company_cac_number',
            'company_registration_type',
            'industry',
            'business_description',
            'company_website',
            'company_email',
            'company_phone',
            'company_logo',
        )
        return fields

...

Also, the Registration Type in your portal template does not match the actual options of that selection field.

頭像
捨棄
作者

@Christoph Farnleitner, please can you guide me exacly on how to achieve this?

Just add that method to your controller file.

作者

I did, upgraded the module then tried it again but still the same.

相關帖文 回覆 瀏覽次數 活動
4
2月 17
8214
0
3月 15
3883
6
5月 25
2179
1
7月 19
6392
0
11月 16
3437