Skip to content
  • Categories
  • Tags
  • Popular
  • Users
  • Groups
Skins
  • Light
  • Brite
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (Flatly)
  • No Skin
Collapse

Odoo 中文社区

  1. Home
  2. Categories
  3. Odoo 培训
  4. Odoo的Binary字段如何设置默认值?

Odoo的Binary字段如何设置默认值?

Scheduled Pinned Locked Moved Odoo 培训
binary fieldodoo技术
1 Posts 1 Posters 1.5k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    digitalsatori
    管理员
    wrote on last edited by
    #1

    Odoo的Binary字段默认保存的是Base64转码后的字符串。那么我们该如何为Binary字段设置默认值呢?比如一个默认的图片。答案当然是将图片文件的内容用Base64转码后赋给这个字段的default属性。

    方法1 :

    1. 比如你可以把一个图片用在线工具转码为Base64格式字符串:
    data:image/png;base64,iVRORw0KGgoBBBNSUhEUgAAAAEAAAABCAIAAACQd1PeAAAA.....................
    
    1. 去掉头部的data:image/png;base64,信息,把剩余的内容保存为默认值
    DEFAULT_IMG = 'iVRORw0KGgoBBBNSUhEUgAAAAEAAAABCAIAAACQd1PeAAAA.....................'
    
    class MyModel(models.Model):
        _name = 'my.model'
    
        field_binary = fields.Binary(default=DEFAULT_IMG)
    

    方法2:

    1. 将图片文件保存在模块目录下,比如:
      /my_module/static/img/my_image.png

    2. 设置默认值:

    import base64
    from odoo import models, fields
    from odoo import modules
    
    def get_default_img():
        with open(modules.get_module_resource('my_module', 'static/img', 'my_image.png'),
                  'rb') as f:
            return base64.b64encode(f.read())
    
    class MyModel(models.Model):
        _name = 'my.model'
    
        field_binary = fields.Binary(default=get_default_img())
    

    【上海先安科技】(tony AT openerp.cn)

    1 Reply Last reply
    0

    Hello! It looks like you're interested in this conversation, but you don't have an account yet.

    Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

    With your input, this post could be even better 💗

    Register Login
    Reply
    • Reply as topic
    Log in to reply
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes


    • Login

    • Don't have an account? Register

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Tags
    • Popular
    • Users
    • Groups