Help

0

Is it possible to hide delete button for some rows in a tree view ?

Avatar
user0780987

Hi,

I have a view like this :

    <field name="product_line_ids" nolabel="1">
      <tree version="7.0">
        <field name="product_id" context="{'product_name_display': 'short'}" />
        <field name="name" />
        <field name="price_unit" />
        <field name="product_qty" />
        <field name="uom_id" />
        <field name="total" />
        <field name="is_included" />
        <field name="avg_price" />
        <field name="is_bought" />
        <field name="origin" />
        <field name="budget_line_id" />
        <field name="purchase_order_id" />
        <field name="type" invisible="1" />
      </tree>
      <form version="7.0">
        <group col="4">
          ...
        </group>
      </form>
    </field>

Depending if purchase_order_id is False or not, I would like to enable / disable the delete button on my view.

How can I achieve this ?

Thank you very much

1 Comment
Avatar
Discard
Avatar
user0780987
-

Thank you Bole, This is an acceptable workaround, indeed. Too bad it's not possible to have this behaviour because with your solution, an error will appear on screen. I prefer to restrict user's actions instead of having error messages. Do you know if it's possible to create some function in javascript or jquery to realize client-side validation?

5 Answers
1
Avatar
Aksana Ivanova
Best Answer

You always can do:

<tree string="XX" editable="top"  delete="false">

...

    <button name="unlink" icon="gtk-cancel" type="object" string="Delete"

                                        attrs="{'invisible': [('your_condition', '=', 0)]}"/>

Avatar
Discard
3
Avatar
Bole
Best Answer

Normaly if you need to restrict deletion on objects you would define a view like:
<field name="product_line_ids" nolabel="1">
      <tree version="7.0" delete="false">.....

This will disable delete option completly on view 

There is no way of putting condition on delete function, it is enabled or disabled....

Workaround is...

If you need to check for some value and accordingly allow or not deletion....
You need to define an unlink method in your .py file for that class like:
 

def unlink(self, cr, uid, ids, context=None):
    for line in self.browse(cr, uid, ids):
        if line.purchase_order_id:
            raise osv.except_osv('error!', 'not allowed to delete record with purchase_line_id')
    return super(name_of_class, self).unlink(cr, uid, ids)

This way delete function is enabled, but before actual deletion some data check is made, and if certain condition is met 
(in this case existing purchase_order_id) system will raise error and will not allow deletion of selected records
Instead of raising error you may also remove id wich contains purchase_order_id from ids list and continue with unlinking (deleting) other records that are ok to be deleted... 

hope this helps...
p.s. the syntax of unlink method if for v7, but should work in v8 also 

look for other examles of unlink methods in some classes and expand to your needs

Avatar
Discard
0
Avatar
caterjohn1@gmail.com
Best Answer
def unlink ( self ): 
"" "
Purpose: to prevent deleting of a YOUR_OBJECT in status NAME_OF_YOUR_STATUS
" ""
for order in self : if order.YOUR_CONDTION in ( ' NAME_OF_YOUR_STATUS_1','NAME_OF_YOUR_STATUS_2', 'CAN_HAVE_MULITPLE_CONDITIONS_OR_YOU_CAN_JUST_HAVE_ONE):raiseUserError ('TYPE_YOUR_ERROR_MESSAGE_HERE_FOR_USER_TO_SEE')returnsuper(YOUR_CLASS_NAME,self) .unlink ()



Avatar
Discard
0
Avatar
Agile
Best Answer

You can use this function this is working for me.
def unlink(self, cr, uid, ids, context=None):
    for line in self.browse(cr, uid, ids):
        if line.purchase_order_id:
            raise osv.except_osv('error!', 'not allowed to delete record with purchase_line_id')
    return super(name_of_class, self).unlink(cr, uid, ids)

Avatar
Discard