版权声明:本文为博主原创文章,未经博主允许不得转载。
首先推荐一个qweb制作report的视频,比较直观。
http://v.youku.com/v_show/id_XMTI4OTU0MDQwOA==.html?from=s1.8-1-1.2
然后,转载一篇文章:http://www.jianshu.com/p/f33a236702cb
Before start of introduction, odoo (former openerp) is open source, the source code is the best tutorial.
Eg, you can check point_of_sale module to check out how to write qweb report.
qweb reportintroduction
openerp v7 use webkit and rml, openerp v6 uses rml.
qweb report is the new report engine of odoo V8, webkit and rml is depreciated from v8.
qweb is also rendering engine for odoo web server. Thus, odoo standardise the web engine.
Guide line
This article talks about
- qweb report introduction
- how to create a qweb report
qweb report
main elements
<code class="bash" style="padding:0px; font-family:Menlo,Monaco,Consolas,'Courier New',monospace; font-size:12px; border:none; background-color:transparent">** report registration <span class="hljs-keyword" style="color:rgb(133,153,0)">in</span> database ** report translation registration <span class="hljs-keyword" style="color:rgb(133,153,0)">in</span> database (optional) ** report layout <span class="hljs-keyword" style="color:rgb(133,153,0)">in</span> database ** customize rendering <span class="hljs-built_in" style="color:rgb(38,139,210)">functions</span> (optional)</code>
Qweb introduction
In general ,
- developer write xml code to create actions, report record, views, web layout.
- when installing modules, there actions, reports, views is saved in database.
-
user click a button, open a new windows, it trigger the actions and Web JavaScript API. Web javascript will
- find the view id and read layout code from databass render it into web
- find record id and read record value from database render it into web
-
when print report , odoo will trigger wkhtmltopdf library to render web into pdf document.
odoo qweb introduction
grammer
-
data
t-field
,t-esc
-
loop, condition
<code class="lisp" style="padding:0px; font-family:Menlo,Monaco,Consolas,'Courier New',monospace; font-size:12px; border:none; background-color:transparent"> <p <span class="hljs-literal">t</span>-foreach=<span class="hljs-string" style="color:rgb(147,161,161)">"[1, 2, 3]"</span> <span class="hljs-literal">t</span>-as=<span class="hljs-string" style="color:rgb(147,161,161)">"i"</span>> <t <span class="hljs-literal">t</span>-esc=<span class="hljs-string" style="color:rgb(147,161,161)">"i"</span>/> </p></code>
<code class="xml" style="padding:0px; font-family:Menlo,Monaco,Consolas,'Courier New',monospace; font-size:12px; border:none; background-color:transparent"> <span class="hljs-tag"><<span class="hljs-title" style="color:rgb(38,139,210)">t</span> <span class="hljs-attribute" style="color:rgb(181,137,0)">t-if</span>=<span class="hljs-value" style="color:rgb(42,161,152)">"condition"</span>></span> <span class="hljs-tag"><<span class="hljs-title" style="color:rgb(38,139,210)">p</span>></span>ok<span class="hljs-tag"></<span class="hljs-title" style="color:rgb(38,139,210)">p</span>></span> <span class="hljs-tag"></<span class="hljs-title" style="color:rgb(38,139,210)">t</span>></span></code>
how to create a qweb report
0. moduel structure
<code class="ruby" style="padding:0px; font-family:Menlo,Monaco,Consolas,'Courier New',monospace; font-size:12px; border:none; background-color:transparent"><span class="hljs-class"><span class="hljs-keyword" style="color:rgb(133,153,0)">module</span></span>
| report
| customize_report.py
| views
| report_layout_view.xml
| report.xml
| __init_<span class="hljs-number" style="color:rgb(42,161,152)">_</span>.py
| __openerp_<span class="hljs-number" style="color:rgb(42,161,152)">_</span>.py
| ...</code>
1. create a report
- if no 2nd step, the value of file and name 2nd step.
- if 2nd step, the value of should be the template id in 2nd step
<code class="objectivec" style="padding:0px; font-family:Menlo,Monaco,Consolas,'Courier New',monospace; font-size:12px; border:none; background-color:transparent"><report <span class="hljs-keyword" style="color:rgb(133,153,0)">id</span>=<span class="hljs-string" style="color:rgb(42,161,152)">"report_sale_order_libiya_xxx"</span> string=<span class="hljs-string" style="color:rgb(42,161,152)">"Sale Order Libiya"</span> model=<span class="hljs-string" style="color:rgb(42,161,152)">"sale.order"</span> report_type=<span class="hljs-string" style="color:rgb(42,161,152)">"qweb-pdf"</span> file=<span class="hljs-string" style="color:rgb(42,161,152)">"module.report_sale_order_xxx"</span> name=<span class="hljs-string" style="color:rgb(42,161,152)">"module.report_sale_order_xxx"</span> /></code>
2. create a report translation (optional)
<code class="xml" style="padding:0px; font-family:Menlo,Monaco,Consolas,'Courier New',monospace; font-size:12px; border:none; background-color:transparent"><span class="hljs-tag"><<span class="hljs-title" style="color:rgb(38,139,210)">template</span> <span class="hljs-attribute" style="color:rgb(181,137,0)">id</span>=<span class="hljs-value" style="color:rgb(42,161,152)">"report_sale_order_xxx"</span>></span> <span class="hljs-tag"><<span class="hljs-title" style="color:rgb(38,139,210)">t</span> <span class="hljs-attribute" style="color:rgb(181,137,0)">t-call</span>=<span class="hljs-value" style="color:rgb(42,161,152)">"report.html_container"</span>></span> <span class="hljs-tag"><<span class="hljs-title" style="color:rgb(38,139,210)">t</span> <span class="hljs-attribute" style="color:rgb(181,137,0)">t-foreach</span>=<span class="hljs-value" style="color:rgb(42,161,152)">"doc_ids"</span> <span class="hljs-attribute" style="color:rgb(181,137,0)">t-as</span>=<span class="hljs-value" style="color:rgb(42,161,152)">"doc_id"</span>></span> <span class="hljs-tag"><<span class="hljs-title" style="color:rgb(38,139,210)">t</span> <span class="hljs-attribute" style="color:rgb(181,137,0)">t-raw</span>=<span class="hljs-value" style="color:rgb(42,161,152)">"translate_doc(doc_id, doc_model, 'partner_id.lang', 'module.report_sale_order_xxx_document')"</span>/></span> <span class="hljs-tag"></<span class="hljs-title" style="color:rgb(38,139,210)">t</span>></span> <span class="hljs-tag"></<span class="hljs-title" style="color:rgb(38,139,210)">t</span>></span> <span class="hljs-tag"></<span class="hljs-title" style="color:rgb(38,139,210)">template</span>></span></code>
create report layout
odoo is using bootstrap for web layout
http://www.w3cschool.cc/bootstrap/bootstrap-grid-system.html
<code class="xml" style="padding:0px; font-family:Menlo,Monaco,Consolas,'Courier New',monospace; font-size:12px; border:none; background-color:transparent"><span class="hljs-tag"><<span class="hljs-title" style="color:rgb(38,139,210)">template</span> <span class="hljs-attribute" style="color:rgb(181,137,0)">id</span>=<span class="hljs-value" style="color:rgb(42,161,152)">"report_sale_order_xxx_document"</span>></span> <span class="hljs-tag"><<span class="hljs-title" style="color:rgb(38,139,210)">t</span> <span class="hljs-attribute" style="color:rgb(181,137,0)">t-call</span>=<span class="hljs-value" style="color:rgb(42,161,152)">"report.external_layout"</span>></span> <span class="hljs-tag"><<span class="hljs-title" style="color:rgb(38,139,210)">div</span> <span class="hljs-attribute" style="color:rgb(181,137,0)">class</span>=<span class="hljs-value" style="color:rgb(42,161,152)">"page"</span>></span> <span class="hljs-tag"><<span class="hljs-title" style="color:rgb(38,139,210)">div</span> <span class="hljs-attribute" style="color:rgb(181,137,0)">class</span>=<span class="hljs-value" style="color:rgb(42,161,152)">"oe_structure"</span>/></span> <span class="hljs-tag"><<span class="hljs-title" style="color:rgb(38,139,210)">table</span> <span class="hljs-attribute" style="color:rgb(181,137,0)">class</span>=<span class="hljs-value" style="color:rgb(42,161,152)">"dest_address"</span>></span> <span class="hljs-tag"><<span class="hljs-title" style="color:rgb(38,139,210)">tr</span>></span> <span class="hljs-tag"><<span class="hljs-title" style="color:rgb(38,139,210)">td</span>></span> <span class="hljs-tag"><<span class="hljs-title" style="color:rgb(38,139,210)">strong</span>></span>Customer address:<span class="hljs-tag"></<span class="hljs-title" style="color:rgb(38,139,210)">strong</span>></span> <span class="hljs-tag"><<span class="hljs-title" style="color:rgb(38,139,210)">div</span> <span class="hljs-attribute" style="color:rgb(181,137,0)">t-field</span>=<span class="hljs-value" style="color:rgb(42,161,152)">"o.partner_id"</span> <span class="hljs-attribute" style="color:rgb(181,137,0)">t-field-options</span>=<span class="hljs-value" style="color:rgb(42,161,152)">'{"widget": "contact", "fields": ["address", "name", "phone", "fax","email","vat"], "no_marker": false}'</span>/></span> <span class="hljs-tag"><<span class="hljs-title" style="color:rgb(38,139,210)">p</span> <span class="hljs-attribute" style="color:rgb(181,137,0)">t-if</span>=<span class="hljs-value" style="color:rgb(42,161,152)">"o.partner_id.vat"</span>></span>VAT: <span class="hljs-tag"><<span class="hljs-title" style="color:rgb(38,139,210)">span</span> <span class="hljs-attribute" style="color:rgb(181,137,0)">t-field</span>=<span class="hljs-value" style="color:rgb(42,161,152)">"o.partner_id.vat"</span>/></span><span class="hljs-tag"></<span class="hljs-title" style="color:rgb(38,139,210)">p</span>></span> <span class="hljs-tag"></<span class="hljs-title" style="color:rgb(38,139,210)">td</span>></span> <span class="hljs-tag"></<span class="hljs-title" style="color:rgb(38,139,210)">tr</span>></span> <span class="hljs-tag"></<span class="hljs-title" style="color:rgb(38,139,210)">table</span>></span> <span class="hljs-tag"><<span class="hljs-title" style="color:rgb(38,139,210)">div</span> <span class="hljs-attribute" style="color:rgb(181,137,0)">class</span>=<span class="hljs-value" style="color:rgb(42,161,152)">"row mt32 mb32"</span> <span class="hljs-attribute" style="color:rgb(181,137,0)">id</span>=<span class="hljs-value" style="color:rgb(42,161,152)">"informations"</span>></span> <span class="hljs-tag"><<span class="hljs-title" style="color:rgb(38,139,210)">div</span> <span class="hljs-attribute" style="color:rgb(181,137,0)">t-if</span>=<span class="hljs-value" style="color:rgb(42,161,152)">"o.client_order_ref"</span> <span class="hljs-attribute" style="color:rgb(181,137,0)">class</span>=<span class="hljs-value" style="color:rgb(42,161,152)">"col-xs-3"</span>></span> <span class="hljs-tag"><<span class="hljs-title" style="color:rgb(38,139,210)">strong</span>></span>Invoice:<span class="hljs-tag"></<span class="hljs-title" style="color:rgb(38,139,210)">strong</span>></span> <span class="hljs-tag"><<span class="hljs-title" style="color:rgb(38,139,210)">p</span> <span class="hljs-attribute" style="color:rgb(181,137,0)">t-field</span>=<span class="hljs-value" style="color:rgb(42,161,152)">"o.client_order_ref"</span>/></span> <span class="hljs-tag"></<span class="hljs-title" style="color:rgb(38,139,210)">div</span>></span> <span class="hljs-tag"><<span class="hljs-title" style="color:rgb(38,139,210)">div</span> <span class="hljs-attribute" style="color:rgb(181,137,0)">t-if</span>=<span class="hljs-value" style="color:rgb(42,161,152)">"o.user_id.name"</span> <span class="hljs-attribute" style="color:rgb(181,137,0)">class</span>=<span class="hljs-value" style="color:rgb(42,161,152)">"col-xs-3"</span>></span> <span class="hljs-tag"><<span class="hljs-title" style="color:rgb(38,139,210)">strong</span>></span>Salesperson:<span class="hljs-tag"></<span class="hljs-title" style="color:rgb(38,139,210)">strong</span>></span> <span class="hljs-tag"><<span class="hljs-title" style="color:rgb(38,139,210)">p</span> <span class="hljs-attribute" style="color:rgb(181,137,0)">t-field</span>=<span class="hljs-value" style="color:rgb(42,161,152)">"o.user_id.name"</span>/></span> <span class="hljs-tag"></<span class="hljs-title" style="color:rgb(38,139,210)">div</span>></span> <span class="hljs-tag"><<span class="hljs-title" style="color:rgb(38,139,210)">div</span> <span class="hljs-attribute" style="color:rgb(181,137,0)">t-if</span>=<span class="hljs-value" style="color:rgb(42,161,152)">"o.payment_term"</span> <span class="hljs-attribute" style="color:rgb(181,137,0)">class</span>=<span class="hljs-value" style="color:rgb(42,161,152)">"col-xs-3"</span>></span> <span class="hljs-tag"><<span class="hljs-title" style="color:rgb(38,139,210)">strong</span>></span>Payment Term:<span class="hljs-tag"></<span class="hljs-title" style="color:rgb(38,139,210)">strong</span>></span> <span class="hljs-tag"><<span class="hljs-title" style="color:rgb(38,139,210)">p</span> <span class="hljs-attribute" style="color:rgb(181,137,0)">t-field</span>=<span class="hljs-value" style="color:rgb(42,161,152)">"o.payment_term"</span>/></span> <span class="hljs-tag"></<span class="hljs-title" style="color:rgb(38,139,210)">div</span>></span> <span class="hljs-tag"></<span class="hljs-title" style="color:rgb(38,139,210)">div</span>></span> <span class="hljs-tag"></<span class="hljs-title" style="color:rgb(38,139,210)">template</span>></span></code>
create customize report rending function
There are two ways to registrating
method 1
odoo use this way to reuse the code of v7.
<code class="python" style="padding:0px; font-family:Menlo,Monaco,Consolas,'Courier New',monospace; font-size:12px; border:none; background-color:transparent"><span class="hljs-keyword" style="color:rgb(133,153,0)">import</span> time
<span class="hljs-keyword" style="color:rgb(133,153,0)">from</span> openerp.report <span class="hljs-keyword" style="color:rgb(133,153,0)">import</span> report_sxw
<span class="hljs-keyword" style="color:rgb(133,153,0)">from</span> openerp.osv <span class="hljs-keyword" style="color:rgb(133,153,0)">import</span> osv
<span class="hljs-class"><span class="hljs-keyword" style="color:rgb(133,153,0)">class</span> <span class="hljs-title" style="color:rgb(181,137,0)">sale_report_xxx</span><span class="hljs-params">(report_sxw.rml_parse)</span>:</span>
<span class="hljs-function"><span class="hljs-keyword" style="color:rgb(133,153,0)">def</span> <span class="hljs-title" style="color:rgb(38,139,210)">_print_test</span><span class="hljs-params">(self)</span>:</span>
<span class="hljs-keyword" style="color:rgb(133,153,0)">return</span> <span class="hljs-string" style="color:rgb(42,161,152)">"good"</span>
<span class="hljs-function"><span class="hljs-keyword" style="color:rgb(133,153,0)">def</span> <span class="hljs-title" style="color:rgb(38,139,210)">__init__</span><span class="hljs-params">(self, cr, uid, name, context)</span>:</span>
super(sale_report_libiya, self).__init__(cr, uid, name, context=context)
self.localcontext.update({
<span class="hljs-string" style="color:rgb(42,161,152)">'time'</span>: time,
<span class="hljs-string" style="color:rgb(42,161,152)">'cr'</span>:cr,
<span class="hljs-string" style="color:rgb(42,161,152)">'uid'</span>: uid,
<span class="hljs-string" style="color:rgb(42,161,152)">'curr_rec'</span>: self.curr_rec,
<span class="hljs-string" style="color:rgb(42,161,152)">'compute_currency'</span>: self.compute_currency,
<span class="hljs-string" style="color:rgb(42,161,152)">'print_test'</span>: self._print_test,
<span class="hljs-string" style="color:rgb(42,161,152)">'print_test2'</span>: <span class="hljs-string" style="color:rgb(42,161,152)">"good2"</span>,
<span class="hljs-string" style="color:rgb(42,161,152)">'other_methods'</span>self._other_methods,
})
<span class="hljs-class"><span class="hljs-keyword" style="color:rgb(133,153,0)">class</span> <span class="hljs-title" style="color:rgb(181,137,0)">report_pos_details</span><span class="hljs-params">(osv.AbstractModel)</span>:</span>
_name = <span class="hljs-string" style="color:rgb(42,161,152)">'report.sale_webkit_report_libiya.report_sale_order_xxx'</span>
_inherit = <span class="hljs-string" style="color:rgb(42,161,152)">'report.abstract_report'</span>
_template = <span class="hljs-string" style="color:rgb(42,161,152)">'module.report_sale_order_xxx'</span>
_wrapped_report_class = sale_report_xxx</code>
Method 2
code taken from odoo documentation
<code class="python" style="padding:0px; font-family:Menlo,Monaco,Consolas,'Courier New',monospace; font-size:12px; border:none; background-color:transparent"><span class="hljs-keyword" style="color:rgb(133,153,0)">from</span> openerp <span class="hljs-keyword" style="color:rgb(133,153,0)">import</span> api, models
<span class="hljs-class"><span class="hljs-keyword" style="color:rgb(133,153,0)">class</span> <span class="hljs-title" style="color:rgb(181,137,0)">ParticularReport</span><span class="hljs-params">(models.AbstractModel)</span>:</span>
_name = <span class="hljs-string" style="color:rgb(42,161,152)">'report.<<module.reportname>>'</span>
<span class="hljs-decorator" style="color:rgb(38,139,210)"> @api.multi</span>
<span class="hljs-function"><span class="hljs-keyword" style="color:rgb(133,153,0)">def</span> <span class="hljs-title" style="color:rgb(38,139,210)">render_html</span><span class="hljs-params">(self, data=None)</span>:</span>
report_obj = self.env[<span class="hljs-string" style="color:rgb(42,161,152)">'report'</span>]
report = report_obj._get_report_from_name(<span class="hljs-string" style="color:rgb(42,161,152)">'<<module.reportname>>'</span>)
docargs = {
<span class="hljs-string" style="color:rgb(42,161,152)">'doc_ids'</span>: self._ids,
<span class="hljs-string" style="color:rgb(42,161,152)">'doc_model'</span>: report.model,
<span class="hljs-string" style="color:rgb(42,161,152)">'docs'</span>: self,
}
<span class="hljs-keyword" style="color:rgb(133,153,0)">return</span> report_obj.render(<span class="hljs-string" style="color:rgb(42,161,152)">'<<module.reportname>>'</span>, docargs)</code>
tips
website editor
After this module installed, website manager can edit report online once report type is set in backend to html.
After the online edit, the report can be changed back to pdf to print pdf document later.
output image on Qweb report
除了以上问题外,还有一个经常遇到的问题就是在报表中输出image,这里也来介绍个方法:Now I'm printing the image in the QWeb report this way:
- <span t-field="product_id.image_small" t-field-options="{"widget": "image", "class": "img-rounded"}"/>
说是原创,实在无语,多多见凉。