如何在树视图中上传图像并将其显示在Openerp 7或Odoo的树或列表视图中

时间:2015-03-18 08:34:03

标签: openerp openerp-7 openerp-8

我使用了下面的代码并单击了save。它保存文件,但是单击保存的文件。它以bin文件格式下载。

我有两个疑问:

  1. 如何使用上传的相同文件名和扩展名下载它。
  2. 如何查看树状视图中上传的所有图像。使用widget =“image”不起作用。
  3. openerp .py文件:

    {
        'name' : 'Vyas File Upload Demo',
        'version' : '0.1',
        'category' : 'Demo',
        'description' : 
        """
            Module to test the working of File upload and download in Openerp 7
    
        """,
        'author' : 'Vyas Senthil',
        'license' : 'AGPL-3',
        'depends' : [],
        'init_xml' : [],
        'demo_xml' : [],
        'update_xml' : ['file_upload_demo_view.xml'],
        'active': False,
        'installable': True,
    }
    

    文件上传demo.py [Python文件]:

    from osv import osv,fields
        from datetime import date,datetime
        from openerp import netsvc
        from openerp import SUPERUSER_ID
        from openerp.tools.translate import _
        from operator import itemgetter
        import base64
        import time
        import re
        import psycopg2 as psy
        import sys
        import pprint   # To print python objects in a readable format , use "pprint.pprint(obj or dictionary)".
        import pdb  # To debug python files. Use pdb.set_trace()
    
    
        class manage_inspection(osv.osv):
            ''' Inspection module'''
            _name = 'manage.inspection'
            _columns = {
    
                'name': fields.char('Inspection Number',required=True), # Inspection Number
                'file_upload_demo_grid': fields.one2many('file.upload.demo','manage_inspection_id', 'File upload Grid'), 
            }
    
    
        class file_upload_demo(osv.osv):
            #class to maintain plant type 
    
            _name = 'file.upload.demo'
    
    
            def _get_contract_file_name(self, cr, uid, ids, field_name, arg, context=None):
                ''' To set the name_filename field'''
    
                result = {}
                for product_data in self.browse(cr, uid, ids, context=context):
                        result[product_data.id] = product_data['file_path']
                        return result
                return result
    
    
            _columns = {
    
                    'image_upload': fields.binary(string='Image Upload new'),
                    'file_path': fields.char('File Name and Extension', size=128),
                    'name_filename':fields.function(_get_contract_file_name, type="char", size=255, method=True, string="File Name"),
    
                    'manage_inspection_id': fields.many2one('manage.inspection','Manage Inspection Id'), # Connection to Parent form.
    
    
                }
    
        file_upload_demo()
    

    file_upload_demo_view.xml [XML文件]:

    <?xml version="1.0" ?>
    <openerp>
        <data>
    
            <!-- Top Menu -->
            <menuitem name="Vyas File Upload Demo"
                id="file_upload_demo_top_menu"
                sequence="32"/>
    
            <!-- Side Menu Heading -->
            <menuitem name="File Upload Demo"
                id="file_upload_demo_side_heading"
                parent="file_upload_demo_top_menu"
                sequence="1"/>
    
    
    
            <!-- inspection form view -->
            <record model="ir.ui.view" id="view_file_upload_demo_form">
                <field name="name">manage.inspection.form</field>
                <field name="model">manage.inspection</field>
                <field name="arch" type="xml">
                    <form string="Manage Inspection" version="7.0">
    
                        <!-- File Upload method -->
                        <field name="name" placeholder="Inspection Number" style="width:30%%" />
    
                        <!-- Grid - Upload Photos -->
                        <field name="file_upload_demo_grid" />
    
                    </form>
                </field>
            </record>
    
            <!-- file_upload_demo tree view -->
            <record model="ir.ui.view" id="view_file_upload_demo_tree">
                <field name="name">file.upload.demo.tree</field>
                <field name="model">file.upload.demo</field>
                <field name="arch" type="xml">
                    <tree string="file upload demo" editable="top">
    
                        <!-- File Upload method -->
                        <field name="file_path" />
                        <field name="image_upload"  filename="file_path" />
                    </tree>
                </field>
            </record>
    
    
            <!-- action menu -->
            <record model="ir.actions.act_window" id="action_upload_files_demo">  
                <field name="name">Inspection </field>          <!-- view label -->
                <field name="res_model">manage.inspection</field>
                <field name="view_type">form</field>
                <field name="view_mode">tree,form</field>
            </record>
    
            <!-- Side menu to open Manage Sales Invoice -->
            <menuitem 
                name="Upload Files Demo"
                id="upload_file_demo"
                parent="file_upload_demo_side_heading"
                sequence="3"
                action="action_upload_files_demo"/>
        </data>
    </openerp>
    

1 个答案:

答案 0 :(得分:0)

在项目模块中查找文档按钮这允许您附加多个附件并将其视为树,查看保存附件名称和扩展名的最美丽的东西(尝试这样做)
注意: 此附件可以是图像或任何类型的附件