Hello all,
I have created a new postgresql view which has the same code than account.invoice.report (totally duplicated). We took the code on Odoo 8. So this report is in the old api.
I want to create a new column in the graph associated to this view : invoice week. Invoice_week is the week in the year of the date_invoice for each invoice.
<record id="view_account_invoice_report_graph_vtm2" model="ir.ui.view">
<field name="name">account.invoice.report.graph.vtm2</field>
<field name="model">account.invoice.report.vtm2</field>
<field name="arch" type="xml">
<graph string="Invoices Analysis" type="pivot">
<field name="fiscal_position" type="row"/>
<field name="invoice_week" type="col"/>
<field name="price_total" type="measure"/>
<field name="nbr" type="measure"/>
</graph>
</field>
</record>
So, I have created a new computed field on the report model. I wanted the invoice_week computed on the fly when the report is generated. My function _finddate is ok and can extract the week of an invoice according to his date_invoice :
'invoice_week' : fields.function(_finddate, string="Semaine de la facture", type='char', readonly=True),
But I get this error :
AssertionError: Fields in 'groupby' must be regular database-persisted fields (no function or
related fields), or function fields with store=True
My question is : do I have to imperatively create a new stored field on the account.invoice model or it is possible to calculate the invoice_week on the fly according to the date_invoice?
Or : Do postgresql views really work only with stored field somewhere in the database?
EDIT #1
If I add a new field 'invoice_week' on account.invoice model, it would be computed like this. So it would be the week of the year calculated with the date_invoice :
from openerp import models, fields, api, _
from datetime import datetime
class account_invoice(models.Model):
_inherit = "account.invoice"
invoice_week = fields.Char(compute='_findweek',string="Semaine de la facture", readonly=True, store=True)
@api.one
@api.depends('date_invoice')
def _findweek(self):
for invoice in self:
if invoice.date_invoice:
invoice.invoice_week = datetime.strptime(invoice.date_invoice, "%Y-%m-%d").strftime('%U')