Search code examples
odooodoo-10odoo-11odoo-12odoo-13

Cant figure out how to import one2many and many2one models


Here's the code of a custom model i made, the first class is a model of stocks and general information. The second class contains the daily prices of each of the stocks in the first class for 10 days. The relational field that connects the two is the name of the stock.

I've done a lot of research in order to import the models but i seem to not be able to make it work, any help would be massively appreciated.

class ticker_info(models.Model):
    _name = 'ticker.rep'
    _description = 'Repository of stocks'

    name = fields.Many2one('ticker.db', string = 'Tickers')
    Sector = fields.Char()
    Symbol = fields.Char()


class ticker_prices(models.Model):
    _name = 'ticker.db'
    _description = 'Database of historical data'

    name = fields.Char(string = 'Tickers')
    Date = fields.Datetime()
    Close = fields.Float(digits=(9, 5))

Edit: For each stock in class 1 it needs to be connected to model 2 where i can see 10 daily prices of that stock.


Solution

  • for one2many field you've to create a cyclic relation for example:

    from odoo import models, fields, api

    class ticker_info(models.Model):

    _name = 'ticker.rep'
    
    _description = 'Repository of stocks'
    
    field_o2m_ids = fields.One2many('ticker.db', 'ticker_rep_id', string="Name of field")
    

    class ticker_prices(models.Model):

    _name = 'ticker.db'
    _description = 'Database of historical data'
    
    ticker_rep_id = fields.Many2one('ticker.rep', string="Name of field")