简体   繁体   中英

Remove 'Create and Edit' from many2one field(lot_id) for type 'outgoing' using fields_view_get method

Is there a way that I can remove 'create and edit' in many2one field (lot_id) when the 'type' is 'outgoing' (for delivery - Sales Order)?

EDIT:

Modified:

@api.model
def fields_view_get(self, view_id=None, view_type='form', context=None, toolbar=False, submenu=False):
    res = super(stock_transfer_details, self).fields_view_get(view_id=view_id, view_type=view_type, context=context, toolbar=toolbar, submenu=submenu)
    #codes here

            if view_type == 'form' and is_outgoing == 'outgoing':
               doc = etree.XML(res['arch'])
               for node in doc.xpath("//field[@name='lot_id']"):
                  node.set('no_create', "true")
                  node.set('no_create_edit', "true")
                  setup_modifiers(node, res['fields']['lot_id'])
            res['arch'] = etree.tostring(doc)
    return res

stock_transfer_details.xml (original)

<record id="view_stock_enter_transfer_details" model="ir.ui.view">
  <field name="name">Enter transfer details</field>
  <field name="model">stock.transfer_details</field>
  <field name="arch" type="xml">
    <form string="Transfer details" version="7">
      <field name="picking_source_location_id" invisible="True"/>
      <field name="picking_destination_location_id" invisible="True"/>
        <group string="Products To Move">
          <div class="oe_grey" groups="stock.group_tracking_lot">
                Setting a product and a source package means that the product will be taken
                out of the package.  
          </div>
        </group>
        <group>
          <field name="item_ids"
              context="{'default_sourceloc_id':picking_source_location_id,
                          'default_destinationloc_id':picking_destination_location_id}" nolabel="1">
            <tree string="Inventory Details" editable="bottom" >
              <field name="package_id" groups="stock.group_tracking_lot"/>
              <field name="product_id" required="True"  context="{'uom':product_uom_id}" on_change="product_id_change(product_id,product_uom_id,context)"/>
              <field name="quantity"/>
              <button name="split_quantities" string="Split" type="object" icon="STOCK_PREFERENCES" attrs="{'invisible': [('quantity', '=', 1)]}"/>
              <field name="product_uom_id" options="{&quot;no_open&quot;: True}" groups="product.group_uom"/>
              <field name="sourceloc_id" domain="[('id', 'child_of', parent.picking_source_location_id)]"/>
              <field name="destinationloc_id" domain="[('id', 'child_of', parent.picking_destination_location_id)]"/>
              <field name="result_package_id" groups="stock.group_tracking_lot" context="{'location_id': destinationloc_id}"/>
              <button name="put_in_pack" string="Pack" type="object" icon="terp-product" attrs="{'invisible': [('result_package_id', '!=', False)]}" groups="stock.group_tracking_lot"/>
              <field name="lot_id" groups="stock.group_production_lot" domain="[('product_id','=?', product_id)]" context="{'product_id': product_id}"/>
          </tree>
        </field>
      </group>....

Still not working. How can I locate lot_id field in tree view?

many2one widget (default)

Options : Other possible options you can use with this widget.

  • no_quick_create - remove the Create and edit... option.
  • no_create_edit - remove the Create "foo" option.
  • no_create - no_quick_create and no_create_edit combined.
  • no_open - in read mode: do not render as a link.

Example:

<field name="field_name" options="{'no_quick_create': True, 'no_create_edit' : True}"/>

You can refer it from Ludwik Trammer's post

Alessandro Ruffolo suggested right way you may follow it.

What I'do is to change the context of the model when the form is called (eg into the action), to pass the type (incoming/outgoing) and then overriding fields_view_get in order to modify the field lot_id adding the options attribute as @user00000341 said, only when type is 'outgoing'.

def fields_view_get(self, cr, uid, view_id=None, view_type='form', context=None, toolbar=False, submenu=False):
    res = super(material_paper, self).fields_view_get(cr, uid, view_id=view_id, view_type=view_type, context=context, toolbar=toolbar, submenu=submenu)
    type = context.get('type', False)
    if view_type == 'form' and type == 'outgoing':
        doc = etree.XML(res['fields']['item_ids']['views']['tree']['arch'])
        update = False
        for field in doc.xpath("//field[@name='lot_id']"):
            field.attrib['options'] = "{'no_create_edit': True}"
            update = True
        if update:
            res['fields']['item_ids']['views']['tree']['arch'] = etree.tostring(doc)
    return res

试试这个

<field name="many2one field name" options="{'no_create': True}"/>

I know it's an old question but the answer of Alessandro while being right doesn't really explain the problem here.

Fields view get is a method that will fetch at once almost all the possible views that are required in the form of a tree. The problem is that when you check the arch of the return of fields_view_get you'll see that item_ids is a simple field and there is no tree view attached to it.

The reason is that all sub-views are inserted in their respective field in the fields attribute of the returned value. This is also a recursive method so each sub field can have any number of sub-views attached to them.

One of the reason why Allessandro's answer could fail to work is if the view is already loaded somewhere else in the tree. While looking at res['fields']['item_ids']['views']['tree']['arch'] should work. In more complicated cases, the view could have been loaded previously somewhere else. In those cases, you'd have to check for all the field/views/tree sub trees to change any field that match the field you want to extend using lxml.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM