Skip to Content
เมนู
คุณต้องลงทะเบียนเพื่อโต้ตอบกับคอมมูนิตี้
คำถามนี้ถูกตั้งค่าสถานะ
2 ตอบกลับ
9956 มุมมอง

Is it possible to execute some queries when somedy install my module ?

And is it possible to execute others queries when uninstalling the same module ?

อวตาร
ละทิ้ง
คำตอบที่ดีที่สุด

init_xml that you found is ok.

For an action at installation time, you can also use "function" tag in any normal xml data file, thus calling a function from specified model, see example of "function" tag  in the answer here

For an action at uninstall time (as well as at install time) may work following workaround:

python [8.0 api]:

from openerp import models, fields, api
import logging _logger = logging.getLogger(__name__)

class handle_install_uninstall(models.Model): _name = "handle.install.uninstall"

name = fields.Char('Name')

@api.model
def create(self, vals):
_logger.info("Installing...") # installation time code... # you can use self.env.cr or self._cr here... return super(handle_install_uninstall,self).create(vals)

@api.multi def unlink(self): _logger.info('Uninstalling...") # uninstall time code... # you can use self.env.cr or self._cr here...
return super(handle_install_uninstall,self).unlink()

XML:

<openerp>
    <data>
        <record id="handle_install_uninstall_rec" model="handle.install.uninstall">
<field name="name">Install - uninstall handler</field>
</record> </data> </openerp>

add the above XML file in __openerp__.py manifest as normal 'data' xml file. then record of this model will be created at installation time (consequently "create" method is called and your code executed), when you uninstall module, then record created this way will be deleted at uninstall time, so if this deletion does not bypass the Odoo ORM, then "unlink" will be called and your uninstallation time code will be executed. please make sure that you do not create more then one record of this object in the same database in order to have your code called only once. I never tried this, but normally it should work. Probably there should be better way to call function at uninstallation time, but I do not know such. 


อวตาร
ละทิ้ง
ผู้เขียน

All worked perfectly. When installing AND when uninstalling. Thank you very much.

you're welcome

ผู้เขียน คำตอบที่ดีที่สุด

I've found a part of my answer, to execute SQL query when installing my module, I must add :

'init_xml' : ['query.sql']

in the file __openerp__.py.

But I still don't know how to execute query when uninstalling the module.

อวตาร
ละทิ้ง

Thank you. your comment help me alot

Related Posts ตอบกลับ มุมมอง กิจกรรม
ORM Query Search แก้ไขแล้ว
7
เม.ย. 22
14916
1
มี.ค. 17
6093
2
มี.ค. 15
10708
Can't delete buttons แก้ไขแล้ว
1
ธ.ค. 23
4244
3
ม.ค. 20
37768