Search code examples
python-3.xxmlodoo-11

Value in tree view inside form not appearing Odoo 11


I just starting to using odoo 11. I'm using tree inside FOLDER form, for displaying data of one2many field. The data inside tree view will be created when I click SYNC button. That button will trigger external api get method and then create appropriate data according to JSON value. When I click the button it triggered the method and successfully entered the database but when I look into the tree view, there is no value. Please help, I am stuck.

PYTHON

class GetFolder(models.Model):
    _name = 'get_folders'
     name_folder = fields.Text(string='Name')
     email_blacklist = fields.Integer(string='Email Blacklist',store=True)
     email_subscribers = fields.Integer(string='Email Subscribers',store=True)
     unique_subscribers = fields.Integer(string='Unique Subscribers', store=True)
     email_count = fields.Integer(string='Email', store=True)
     foldersId = fields.Integer(string='FolderId', store=True)
     list_folder_ids = fields.One2many(comodel_name='get_lists', string='Folder', 
     inverse_name='folder_lists', help="")

     @api.multi
     def get_lists_request(self):
     res = self.env['get_lists'].search([('foldersId','=',self.foldersId)])
     for record in res:
        record.ensure_one()
        list_url = ""
        querystring = {"limit":"50","offset":"0","sort":"desc"}
        headers = {
            "Accept": "application/json",
            "api-key": ""
        }

        list_response = requests.request("GET", list_url, headers=headers, params=querystring)
        print(list_response.text)
        list_loads = simplejson.loads(list_response.text)
        _logger.warning(list_loads)
        for list_load in list_loads.get("lists"):
            names = list_load['name']
            ids = list_load['id']
            blacklist = list_load['totalBlacklisted']
            subscribe = list_load['totalSubscribers']
            self.env['get_lists'].sudo().create({
                    'id': self.id,
                    'name': names,
                    'email_subscribers': subscribe,
                    'email_blacklist': blacklist,
                    'listId' : ids,
                    'foldersId' : self.foldersId,
                    })

class GetList(models.Model):
    _name = "get_lists"

    name = fields.Char(string="Name")
    email_subscribers = fields.Integer(string="Total Subscribers", store=True)
    email_blacklist = fields.Integer(string="Total Blacklist", store=True)
    folder_lists = fields.Many2one(comodel_name='get_folders', string='Lists')
    foldersId = fields.Integer(string="Folder Id", related="folder_lists.foldersId", store=True)
    listId = fields.Integer(string="List Id", store=True)
    

XML

   <notebook>
     <page string="Lists">
        <tr style="margin-bottom:100px;">
           <td><button type="object" name="get_lists_request" string="Synch" class="oe_inline oe_button btn-success"/></td>
        </tr>
        <field name="list_folder_ids"/>
     </page>
   </notebook>

EDITED VERSION

record = self.env['get_folders'].search([('id','=',self.id)])
for recx in record:
    self.env['get_lists'].create({
                        'id': self.id,
                        'name': names,
                        'email_subscribers': subscribe,
                        'email_blacklist': blacklist,
                        'listId' : ids,
                        'foldersId': recx.id,
                        })

Yes it is succeed. Sorry for my late reply. After trying and fixing multiple times. I truly looked at your code and find the error. It is actually really simple. I forgot to input the many2one value. You really made my day. Thank you.


Solution

  • You need to pass the primary_key while creating the get_lists object data.

    Code:

    self.env['get_lists'].sudo().create({
                    'id': self.id,
                    'name': names,
                    'email_subscribers': subscribe,
                    'email_blacklist': blacklist,
                    'listId' : ids,
                    'foldersId' : self.foldersId,
                    'folder_lists': self and self.id // set the ref
            })