在 Odoo 12 注册表单中上传文件

Upload File in Signup form Odoo 12

我正在尝试将一个字段 "attachment" 添加到 Odoo 12 的注册表单中。我与您分享我的工作,如果您可以纠正我,请:

在我的 file_one.xml 中:

<template id="auth_signup_fields_extend" inherit_id="auth_signup.fields" name="Signup Fields Extend">
<xpath expr="//div[hasclass('field-confirm_password')]" position="after">
<div class="form-group field-attachment">
<label for="attachment" class="control-label">Attachment</label>
<input type="file" name="attachment"  multiple="true" data-show-upload="true" data-show- 
caption="true" id="project.id" data-show-preview="true" class="form-control" required="required" t- 
att-readonly="'readonly' if only_passwords else None" t-att-autofocus="'autofocus' if login and not 
only_passwords else None"/>
</div>
</xpath>
</template>

在我的 file_2.xml 中:

<data>
<record id="res_partner_form_inherit" model="ir.ui.view">
<field name="name">res.partner.form.inherit</field>
<field name="model">res.partner</field>
<field name="inherit_id" ref="base.view_partner_form"/>
<field name="arch" type="xml">
<xpath expr="//field[@name='function']" position="after">
<field name="attachment"/>
</xpath>
</field>
</record>
</data>

在我的model.py

# -*- coding: utf-8 -*-

from odoo import fields, models

class ResPartner(models.Model):
    _inherit = "res.partner"
    attachment = fields.Binary(string='File of User',attachment=True)

在我的文件中 controller.py :

@http.route('/web/signup', type='http', auth='public', website=True, 
sitemap=False)
def web_auth_signup(self,**post):
    qcontext = self.get_auth_signup_qcontext()
    values = {}
    if post.get('attachment',False):
        Attachments = request.env['ir.attachment']
        name = post.get('attachment').filename      
        file = post.get('attachment')
        project_id = post.get('project_id')
        attachment = file.read() 
        attachment_id = Attachments.sudo().create({
            'name':name,
            'datas_fname': name,
            'res_name': name,
            'type': 'binary',   
            'res_model': 'model.model',
            'datas': attachment.encode('base64'),
        })
        value = {
            'attachment' : attachment_id
        }
        return request.render("fl_auth_signup.users", value)

我看了很多教程,questions/answers 但是我的模块不起作用,目前,我有错误:局部变量 'Attachments' 在赋值前被引用

请帮忙,你能纠正我吗? ...

您将收到以下错误:

local variable 'attachment_id' referenced before assignment , line 52, in web_auth_signup

会出现这个错误,因为下面的代码应该在 if 语句中:

value = {'attachment': attachment_id}

return request.render("fl_auth_signup.users", value)  

您还使用了 *args**kw(在第 75 行)但未声明它们:

return super(AuthSignupHome, self).web_login(*args, **kw)

我查看了auth_signup中的原始代码,发现您更改了web_auth_signup方法的签名。我建议您保持签名不变并使用 kw 而不是 post

@http.route('/web/signup', type='http', auth='public', website=True,
            sitemap=False)
def web_auth_signup(self, *args, **kw):
    qcontext = self.get_auth_signup_qcontext()
    # values = {}
    if kw.get('attachment', False):
        attachments = request.env['ir.attachment']
        name = kw.get('attachment').filename
        file = kw.get('attachment')
        # project_id = kw.get('project_id')
        attachment = file.read()
        attachment_id = attachments.sudo().create({
            'name': name,
            'datas_fname': name,
            'res_name': name,
            'type': 'binary',
            'res_model': 'model.model',
            'datas': base64.b64encode(attachment),
        })
        value = {
            'attachment': attachment_id
        }
        return request.render("fl_auth_signup.users", value)

        # The rest of your code with no changes

更新 form 标签并将 enctype 设置为 multipart/form-data,当您使用具有文件上传控件的表单时,这是必需的。

<template id="new_signup" inherit_id="auth_signup.signup" name="New Sign up login">
    <xpath expr="//form" position="attributes">
        <attribute name="enctype">multipart/form-data</attribute>
    </xpath>
</template>