OPENERP 构建动态视图
-
在openerp展示界面通常是通过定义class的view(xml文件)来实现的。
有时这种方法不能支持用户自定义字段的需求,于是就可以通过重写fields_view_get()、 read()来实现。
实例代码<br /><br /># -*- coding: utf-8 -*-<br />from openerp.osv import osv,fields<br />from lxml import etree<br />from openerp import tools<br />from openerp.tools import to_xml<br /><br /><br /><br /><br />class AnalysisQuestionnaireType(osv.osv):<br /> _name = 'analysis.questionnaire.type'<br /> _description = '问卷类型'<br /> _columns = {<br /> 'name': fields.char('名称', size=125, required=True),<br /> 'analysis_set': fields.many2many('analysis.title.set', id1='analysis_questionnaire_type_id',<br /> id2='analysis_title_set_id', string='主题'),<br /> }<br /><br /><br /><br /><br />class AnalysisTitleSet(osv.osv):<br /> _name = "analysis.title.set"<br /> _description = "主题"<br /> _columns = {<br /> "name": fields.char(string="名称", size=125, required=True,),<br /> 'analysis_questionnaire_type': fields.many2many('analysis.questionnaire.type',<br /> id1='analysis_title_set_id',<br /> id2='analysis_questionnaire_type_id',<br /> string='问卷类型'),<br /> "analysis_title": fields.one2many('analysis.title', 'set', '题目'),<br /> }<br /><br /><br /><br /><br />class AnalysisTitle(osv.osv):<br /> _name = "analysis.title"<br /> _description = "题目"<br /> _columns = {<br /> "name": fields.char(string="名称", size=125, required=True),<br /> "note": fields.text("描述"),<br /> "set": fields.many2one("analysis.title.set", string="主题", required=True),<br /> 'type': fields.selection([('multiple_choice_only_one_ans', '单项选择'),<br /> ('multiple_choice_multiple_ans', '多项选择'),<br /> ('matrix_of_choices_only_one_ans', '单选组合'),<br /> ('single_textbox', '单行文本框'),<br /> ('multiple_textbox', '多个单行文本框'),<br /> ('comment', '多行文本框'),<br /> ('date', '日期框'),<br /> ('date_and_time', '日期时间框'),<br /> ('descriptive_text', '描述性文本'),<br /> ('attachment', '附件'),<br /> ('image', '图片'),<br /> ], '题目类型', required=1,),<br /> 'is_require_answer': fields.boolean('必填项'),<br /> 'option_id': fields.one2many('analysis.title.option', 'title_id', '备选答案'),<br /> 'column_heading_ids': fields.one2many('analysis.title.column.heading', 'title_id', '标题'),<br /> 'descriptive_text': fields.text('描述文本'),<br /> }<br /><br /><br /><br /><br />class AnalysisTitleOption(osv.osv):<br /> _name = 'analysis.title.option'<br /> _description = '答案选项'<br /> _columns = {<br /> 'title_id': fields.many2one('analysis.title', '题目', ondelete='cascade'),<br /> 'option': fields.char('答案选项', size=128, required=True),<br /> 'type': fields.selection([('char', '字符型'),<br /> ('date', '日期'),<br /> ('datetime', '日期时间型'),<br /> ('integer', '整数型'),<br /> ('float', '小数型'),<br /> ('selection', '选择型')],<br /> '答案类型',<br /> required=True),<br /><br /><br /> }<br /><br /><br /><br /><br />class AnalysisTitleColumnHeading(osv.osv):<br /> _name = 'analysis.title.column.heading'<br /> _description = '选项标题'<br /> _columns = {<br /> 'name': fields.char('选项标题', size=128, required=True),<br /> 'title_id': fields.many2one('analysis.title', '题目', ondelete='cascade'),<br /> }<br /><br /><br /><br /><br />class AnalysisAnswer(osv.osv):<br /> _name = 'analysis.answer'<br /> _description = '答案'<br /> _columns = {<br /> 'analysis_questionnaire': fields.many2one('analysis.questionnaire', '问卷'),<br /> 'value': fields.text('值')<br /> }<br /><br /><br /><br /><br />class AnalysisQuestionnaire(osv.osv):<br /> _name = 'analysis.questionnaire'<br /> _description = '分析问卷'<br /><br /><br /> _columns = {<br /> "name": fields.char(string='名称', size=125, required=True),<br /> 'type': fields.many2one('analysis.questionnaire.type', string='类型', required=True),<br /> 'title': fields.one2many('analysis.answer', "analysis_questionnaire", "答案"),<br /> }<br /><br /><br /> def default_get(self, cr, uid, fields_list, context=None):<br /> default = super(AnalysisQuestionnaire, self).default_get(cr, uid, fields_list, context=context)<br /> con = context.get('type')<br /> default['type'] = con<br /> return default<br /><br /><br /> def view_init(self, cr, uid, fields_list, context=None):<br /> pass<br /><br /><br /> def fields_view_get(self, cr, uid, view_id=None, view_type='form', context=None, toolbar=False, submenu=False,):<br /> if context is None:<br /> context = {}<br /> result = super(AnalysisQuestionnaire, self).fields_view_get(cr, uid, view_id,<br /> view_type=view_type, context=context,<br /> toolbar=toolbar, submenu=submenu)<br /> if view_type == 'form' and context.has_key('type'):<br /> view = etree.fromstring(result['arch'])<br /> fields = result['fields']<br /> print etree.tostring(view)<br /> notebook = etree.SubElement(view, 'notebook')<br /> sets = self.pool.get('analysis.questionnaire.type').browse(cr, uid, context['type']).analysis_set<br /> q_no = 0<br /> for set in sets:<br /> page = etree.SubElement(notebook, 'page', string=set.name)<br /> for title in set.analysis_title:<br /> q_no += 1<br /> etree.SubElement(page, 'newline')<br /> if title.is_require_answer:<br /> etree.SubElement(page, 'separator', {'string': '*'+tools.ustr(q_no)+'.'+tools.ustr(title.name)})<br /> else:<br /> etree.SubElement(page, 'separator', {'string': tools.ustr(q_no)+'.'+tools.ustr(title.name)})<br /><br /><br /> if title.type == 'multiple_choice_only_one_ans':<br /> parent = etree.SubElement(page, 'group')<br /> selection = []<br /> for option in title.option_id:<br /> selection.append((tools.ustr(option.id), option.option))<br /> fields['title'+'_'+tools.ustr(title.id)+'_selection'] = {'type': 'selection',<br /> 'selection': selection,<br /> 'name': title.name}<br /> etree.SubElement(parent, 'field', {'name': 'title'+'_'+tools.ustr(title.id)+'_selection'})<br /> elif title.type == 'multiple_choice_multiple_ans':<br /> parent = etree.SubElement(page, 'group', {'col': '4', 'colspan': '4'})<br /> for option in title.option_id:<br /> etree.SubElement(parent, 'field', {'name': 'title'+'_'+tools.ustr(title.id)+'-'+'option'+'_'<br /> + tools.ustr(option.id)})<br /> fields['title'+'_'+tools.ustr(title.id)+'-'+'option'+'_'<br /> + tools.ustr(option.id)] = {'type': 'boolean', 'string': option.option}<br /> elif title.type == 'matrix_of_choices_only_one_ans':<br /> parent = etree.SubElement(page, 'group', {'col': '2', 'colspan': '2'})<br /> selection = []<br /> for option in title.option_id:<br /> selection.append((tools.ustr(option.id), option.option))<br /> for col in title.column_heading_ids:<br /> etree.SubElement(parent, 'newline')<br /> etree.SubElement(parent, 'field', {'name': 'title'+'_'+tools.ustr(title.id)+'-'+'col'+'_'<br /> + tools.ustr(col.id),<br /> })<br /> fields['title'+'_'+tools.ustr(title.id)+'-'+'col'+'_'<br /> + tools.ustr(col.id)] = {'type': 'selection', 'selection': selection}<br /> elif title.type == 'single_textbox':<br /> parent = etree.SubElement(page, 'group',)<br /> etree.SubElement(parent, 'field', {'name': 'title'+'_'+tools.ustr(title.id) + "_single",<br /> 'nolabel': "1",<br /> 'colspan': "4"})<br /> fields['title'+'_'+tools.ustr(title.id) + "_single"] = {'type': 'char', 'size': 255}<br /> elif title.type == 'multiple_textbox':<br /> parent = etree.SubElement(page, 'group', {'col': '4', 'colspan': '4'})<br /> for col in title.column_heading_ids:<br /> fields['title'+'_'+tools.ustr(title.id)+'-'+'col'+'_'+tools.ustr(col.id)] = {'type': 'char',<br /> 'size': 255}<br /> etree.SubElement(parent, 'field', {'width': '300',<br /> 'colspan': '1',<br /> 'name': 'title'+'_'+tools.ustr(title.id)+'-'+'col'+'_'<br /> + tools.ustr(col.id)})<br /> elif title.type == 'comment':<br /> parent = etree.SubElement(page, 'group')<br /> etree.SubElement(parent, 'field', {'name': 'title'+'_'+tools.ustr(title.id) + "_comment",<br /> 'nolabel': "1",<br /> 'colspan': "4"})<br /> fields['title'+'_'+tools.ustr(title.id) + "_comment"] = {'type': 'text'}<br /> elif title.type == 'date':<br /> parent = etree.SubElement(page, 'group', {'col': '4', 'colspan': '4'})<br /> for col in title.column_heading_ids:<br /> fields['title'+'_'+tools.ustr(title.id)+'-'+'col'+'_'+tools.ustr(col.id)] = {'type': 'date'}<br /> etree.SubElement(parent, 'field', {'name': 'title'+'_'+tools.ustr(title.id)+'-'+'col'+'_'<br /> + tools.ustr(col.id)})<br /> elif title.type == 'date_and_time':<br /> parent = etree.SubElement(page, 'group', {'col': '4', 'colspan': '4'})<br /> for col in title.column_heading_ids:<br /> fields['title'+'_'+tools.ustr(title.id)+'-'<br /> + 'col'+'_'+tools.ustr(col.id)] = {'type': 'datetime'}<br /> etree.SubElement(parent, 'field', {'name': 'title'+'_'+tools.ustr(title.id)+'-'+'col'+'_'<br /> + tools.ustr(col.id)})<br /> elif title.type == 'attachment':<br /> parent = etree.SubElement(page, 'group',)<br /> fields['title'+'_'+tools.ustr(title.id)+'_attachment'] = {'type': 'binary'}<br /> etree.SubElement(parent, 'field', {'name': 'title'+'_'+tools.ustr(title.id)+'_attachment'})<br /> elif title.type == 'descriptive_text':<br /> parent = etree.SubElement(page, 'group')<br /> if title.descriptive_text:<br /> for que_test in title.descriptive_text.split('\n'):<br /> etree.SubElement(parent, 'label', {'string': to_xml(tools.ustr(que_test)),<br /> 'align': "0.0"})<br /> elif title.type == 'image':<br /> parent = etree.SubElement(page, 'group',)<br /> fields['title'+'_'+tools.ustr(title.id)+'_image'] = {'type': 'binary'}<br /> etree.SubElement(parent, 'field', {'name': 'title'+'_'+tools.ustr(title.id)+'_image',<br /> 'widget': 'image'})<br /> result['arch'] = etree.tostring(view)<br /> return result<br /><br /><br /> def create(self, cr, uid, vals, context=None):<br /> answer_obj = self.pool.get("analysis.answer")<br /> questionnaire_vals = {'name': vals.get('name'), 'type': vals.get('type')}<br /> vals.pop('name', "not name")<br /> vals.pop('type', 'not type')<br /> answer_vals = {}<br /> questionnaire_id = super(AnalysisQuestionnaire, self).create(cr, uid, questionnaire_vals, context=context)<br /> answer_vals['analysis_questionnaire'] = questionnaire_id<br /> answer_vals['value'] = vals<br /> answer_obj.create(cr, uid, answer_vals, context=context)<br /> return questionnaire_id<br /><br /><br /> def read(self, cr, user, ids, fields=None, context=None, load='_classic_read'):<br /> result = super(AnalysisQuestionnaire, self).read(cr, user, ids, fields=fields, context=context, load=load)<br /> answer_obj = self.pool.get('analysis.answer')<br /> i = 0<br /> while i < len(result):<br /> answer_list = answer_obj.search(cr, user, [('analysis_questionnaire', '=', result['id'])])<br /> if answer_list:<br /> val = eval(answer_obj.read(cr, user, answer_list, ['value'], context=context)[0]['value'])<br /> result.update(val)<br /> i += 1<br /> return result<br />
[i]
[color=rgb(69, 69, 69)][font=tahoma][size=14px]上述代码用户可以自己定义问卷题目及题目类型。通过fields_view_get()画出view,然后把动态构建的字段及其值通过重写create()把数据存储到另外一个表里。这里用的是一个text字段把create返回的值直接存储起来的。当需要查看保存过的数据时,通过重写read()整理成需要的数据格式返回。[/size][/font][/color][/i] -
good idea 又学了一招
-
qdfulee你好!感谢分享的代码,如若方便,可否将整体的代码贴出来,或打包发给我【[email protected]】,菜鸟一枚,想在调试的过程中加以学习理解。多谢了~