如何检索现有模型数据并在自定义odoo网站模块页面上显示

时间:2015-11-18 16:22:34

标签: python odoo-8 openerp-8

尝试从model project.project中检索项目数据,并在odoo 8中的自定义网站模块上显示。

坚持通过控制器检索数据。请参阅下面的代码。

models.py

from openerp import models, fields, api

#Just inherit default odoo project.project fields

class project_category(models.Model):

_name='project.project'

_inherit='project.project'

controllers.py

class Website(http.Controller):
@http.route('/index',auth='public',website=True)
def list(self,**kw):
projects = http.request.env['project.project']
print "Test is below "
print projects # this prints project.project() which i think its an empty array. My goal is to retrieve projects and render them on

# template layout
return http.request.render('website.layout',
{'projects':projects.search([])
})

任何建议最受欢迎。谢谢。

2 个答案:

答案 0 :(得分:1)

解决了这个问题。我意识到数组是空的,因为我在调用执行读操作的search()方法之前打印。

您只能在调用projects.search([])

后打印项目

更新了Controllers.py

class Website(http.Controller):
    @http.route('/index',auth='public',website=True)
    def list(self,**kw):
        Projects = http.request.env['project.project']
        print "Test is below "
        arr = Projects.search([])
        print arr
        return http.request.render('website.layout',
               {'projects':Projects.search([])
     })

结果是:

project.project(21,18,20,19,22)

然后我渲染这个:

<强> templates.xml

<t t-foreach="projects" t-as="project">
  <strong><t t-esc="project.name" /></strong>
  <b><t t-esc="project.description" /></b>
 </t>

虽然Odoo呈现了这一点,但现在又出现了新的错误

QWebException:渲染模板'website.layout'时未定义foreach枚举器'projects'

由于

答案 1 :(得分:-1)

var names = new List<string>();
var lastnames = new List<string>();

var text = File.ReadAllLines(@"C:\Users\Jal\Desktop\Test.text");
foreach (var s in text)
{
    if (s.Contains("lastname"))
    {        
        var lastname = s.Substring(s.IndexOf("=") + 1);
        lastnames.Add(lastname);
        continue;
    }
    if (s.Contains("name"))
    {        
        var name = s.Substring(s.IndexOf("=") + 1);
        names.Add(name);
        continue;
    }        
}