跳至內容
選單
此問題已被標幟
3 回覆
2163 瀏覽次數

Hello guys, this code work for me in 1st time but when i try on another database it not work anymore, this code is  about auto create a database if i activate this module, i put code in __init__.py, and how i can convert it to controllers.py?:


# -*- coding: utf-8 -*-
from odoo import api, models
classAutoCreateDB(models.AbstractModel):   
_name ='auto.create.db'
@api.modeldef_create_database(self):       
db_name ='basicdatabase'       
admin_password ='1234'       
demo_data =False


self._create_empty_database(db_name)
self._set_admin_password(admin_password,db_name)

@api.modeldef_create_empty_database(self, db_name):
self.env.cr.create_empty_db(db_name)
@api.modeldef_set_admin_password(self, admin_password,db_name):       
admin_user =self.env['res.users'].sudo().search([('login', '=', 'admin')], limit=1)        admin_user.with_context(force_company=db_name).write({'password': admin_password})
頭像
捨棄
最佳答案

my question too

頭像
捨棄
作者 最佳答案

Thank you for reply me  Neha Kakkar, but i already try this and it not work, it said psycopg2.extensions.cursor object has no attribute 'create_empty_db'. And i can't using self.env.cr cause also got error object has no attribute 'env'. Then i change it too request.env.cr and got error above. 

頭像
捨棄
最佳答案

As far as i understand You can try these steps again,

In your module, add Python file controllers.py and add the necessary dependencies.

from odoo import http
from odoo.http import request

Create a class that inherits from http.Controller for your controller.

class AutoCreateDBController(http.Controller):

To specify the URL endpoint for your controller, add a route decorator.

@http.route('/auto_create_db', type='json', auth='none', methods=['POST'])

Create a method to manage the route, then enclose your code in it.

def create_database(self, db_name, admin_password):
    demo_data = False
    self.env.cr.create_empty_db(db_name)
    admin_user = self.env['res.users'].sudo().search([('login', '=', 'admin')], limit=1)
    admin_user.with_context(force_company=db_name).write({'password': admin_password})
Using self.env and self.env.cr, you can access the env and cr objects within the method, respectively.
Now you can invoke this controller by sending a POST request to the /auto_create_db URL endpoint and passing the db_name and admin_password as JSON data.
I hope this helps you.


頭像
捨棄
相關帖文 回覆 瀏覽次數 活動
0
2月 25
1062
0
1月 25
3
2
11月 24
5494
2
10月 24
2675
0
10月 24
1152