I'm trying to figure out how to tell my module to display the only one possible (due to the given _sql_constraint
) related tag
for a given product (template) in the website (eCommerce product page) in Odoo.
Given the model below, (obviously I need to say) I'm getting an Expected singleton: woa.website.sale.partner.product.tag(1, 2, 3, ...)
error once there is more than one partner having a tag set for a particular product. However, I'm unable to understand how to get the relation between a certain product (i.e. the product page an user is currently looking at) and the partner itself (i.e. the currently logged in website user).
Model:
class WoaWebsiteSalePartnerProductTag(models.Model):
_name = "woa.website.sale.partner.product.tag"
_description = "Allow partners to tag products individually for easier search and reference"
product_id = fields.Many2one('product.template', string='Product', required=True)
partner_id = fields.Many2one('res.partner', string='Customer', required=True)
company_id = fields.Many2one('res.company', string='Company', required=False, default=lambda self: self.env.user.company_id)
tag = fields.Char(string='Product tag given by the partner', required=True, default=False)
_sql_constraints = [
('unique_code', 'unique(product_id, partner_id, company_id)',
'You can define only one tag per product.'),
]
class WoaProductTemplate(models.Model):
_inherit = "product.template"
woa_website_sale_partner_product_tag_product_ids = fields.One2many('woa.website.sale.partner.product.tag', 'product_id')
class WoaPartnerTemplate(models.Model):
_inherit = "res.partner"
woa_website_sale_partner_product_tag_partner_ids = fields.One2many('woa.website.sale.partner.product.tag', 'partner_id')
View:
<template id="woa_website_sale_partner_product_tag.product" inherit_id="website_sale.product">
<xpath expr="//div[@id='product_details']" position="after">
<div class="col-md-6 col-xl-4 offset-xl-2" id="woa_website_sale_partner_product_tag.product.tag">
<t t-if="request.env.user.active == True">
<b>Your reference for this product:</b>
<t t-if="product.woa_website_sale_partner_product_tag_product_ids.partner_id.id == request.env.user.partner_id.id">
<span t-field="product.woa_website_sale_partner_product_tag_product_ids.tag"/>
</t>
</t>
</div>
</xpath>
</template>
I'm quite sure that this is the point where @api.one
and some sweet method actually defining the relation between the product and partner comes in but I don't get how this method is supposed to look like and actually called in the template.
Error message:
Error to render compiling AST
ValueError: Expected singleton: woa.website.sale.partner.product.tag(5, 6)
Template: 1492
Path: /templates/t/t[4]/div/section/div[2]/div[3]/t[1]
Node: <t t-if="product.woa_website_sale_partner_product_tag_product_ids.partner_id.id == request.env.user.partner_id.id">
<span t-field="product.woa_website_sale_partner_product_tag_product_ids.tag"/>
</t>
A computed field (like given below) which does the search()
and return that one record I'm looking for is probably the way to go:
partner_product_tag = fields.Char('woa.website.sale.partner.product.tag', compute='partner_product_tag', store=False)
@api.one
def partner_product_tag(self):
partner_product_tag = request.env['woa.website.sale.partner.product.tag'].search([('product_id', '=', self.product_id), ('partner_id', '=', self.partner_id), ('company_id', '=', self.company_id)])
return partner_product_tag
But, how am I supposed to put this into my module and what would the relevant template section than look like?