Bỏ qua để đến Nội dung
Menu
Câu hỏi này đã bị gắn cờ
3 Trả lời
2365 Lượt xem

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})
Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

my question too

Ảnh đại diện
Huỷ bỏ
Tác giả Câu trả lời hay nhất

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. 

Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

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.


Ảnh đại diện
Huỷ bỏ
Bài viết liên quan Trả lời Lượt xem Hoạt động
0
thg 7 25
1399
0
thg 1 25
3
2
thg 11 24
6149
2
thg 10 24
3124
0
thg 10 24
1403