Skip to Content
Меню
Вам необхідно зареєструватися, щоб взаємодіяти зі спільнотою.
Це запитання позначене
2 Відповіді
12571 Переглядів

I opened a question yesterday (https://www.odoo.com/pt_BR/forum/help-1/question/how-to-keep-multiple-many2many-fields-on-the-same-model-145401) about how to have multiple Many2many fields. Now I am encountering the same problem with One2many fields, but can't get that right because I can't inform table_name as a relation.

My two models are the following:

class SubscriptionBasket(models.Model):
    _name="subscription.basket"
    name = fields.Char("Nome", required=True)
    date_start = fields.Date("Data Inicial", required=True)
    date_end = fields.Date("Data Final", required=True)
    sales_applied = fields.One2many(
        string=u'Vendas Aplicadas',
        comodel_name='sale.order',
        inverse_name='basket_id',
    )
    legumes = fields.One2many(
        string=u'Legumes',
        comodel_name='subscription.basket.line',
        inverse_name='basket_id',
    )
    frutas = fields.One2many(
        string=u'Frutas',
        comodel_name='subscription.basket.line',
        inverse_name='basket_id',
    )
    verduras = fields.One2many(
        string=u'Verduras',
        comodel_name='subscription.basket.line',
        inverse_name='basket_id',
    )
    temperos = fields.One2many(
        string=u'Temperos',
        comodel_name='subscription.basket.line',
        inverse_name='basket_id',
    )

class SubscriptionBasketItem(models.Model):
    _name = "subscription.basket.line"

    name = fields.Char("Itens da Cesta")
    basket_id = fields.Many2one(
        string=u'Cesta',
        comodel_name='subscription.basket',
    )

    product_id = fields.Many2one(
        comodel_name='product.product',
        string='Produto',
    )

    min_quantity = fields.Integer("Quantidade Mínima Disponível", default = 0)

When assigning different subscription.basket.line's to legumes fields to a subscription.basket record, for example, after creating the record all fields will have the same values, which is not the behavior I expect.  How do I get this right on One2many fields, so each field will have its own list of subscription.basket.line itens?

As an example, I overwrote the create function to get logs:

@api.model
def create(self, vals):
    _logger.info("Vals received on creation: %s", vals)
    res = super(SubscriptionBasket, self).create(vals)
    _logger.info("Field temperos.product_id on creation response: %s", res.temperos.product_d)
    return res


Then, when creating a record, informing a product just to the fields "legumes", I get the following log for the vals I received:

    Vals received on creation: {'date_end': '2019-02-06', 'name': 'Cesta', 'date_start': '2019-02-05', 'legumes': [[0,     'virtual_609', {'product_id': 1000, 'min_quantity': 0}]]}

And the following log for the field "temperos", which I would expect to be empty, on the creation response:

   Field temperos.product_id on creation response: product.product(1000,)


Аватар
Відмінити
Найкраща відповідь

Hi,,

maybe you can use related or compute fields

legumes = fields.One2many(
    string=u'Legumes',
    comodel_name='subscription.basket.line',
    inverse_name='basket_id',
)
frutas = fields.One2many(
    string=u'Frutas',
    comodel_name='subscription.basket.line',
    related="legumes",
)
verduras = fields.One2many(
    string=u'Verduras',
    comodel_name='subscription.basket.line',
    compute="_compute_value"
)
temperos = fields.One2many(
    string=u'Temperos',
    comodel_name='subscription.basket.line',
    compute="_compute_value"
)

def _compute_value(self):
    for rec in self:
        rec.temperos = rec.legumes


Аватар
Відмінити
Автор

Sorry, I think that I miss explained myself. Fields containing always the same values is the current behavior, but is not what I expect. My intent is to each field to have its own list of "subscription.basket.line" itens.

So you must define some unique criteria to identify the record based on those fields wich related to subscription.basket.line

For ex:

class SubscriptionBasketItem(models.Model):

_name = "subscription.basket.line"

# **** Your fields

data_type = fields.Selection([('legumes','Legumes'), ('frutas','Frutas'), ('verduras','Verduras'), ('temperos','Temperos')], required=True, string="Some Type")

then you can write normal one2many fields like this in SubscriptionBasket model

legumes = fields.One2many(

string=u'Legumes',

comodel_name='subscription.basket.line',

inverse_name='basket_id',

domain=[('data_type','=','legumes')]

)

frutas = fields.One2many(

string=u'Frutas',

comodel_name='subscription.basket.line',

inverse_name='basket_id',

domain=[('data_type','=','frutas')]

)

verduras = fields.One2many(

string=u'Verduras',

comodel_name='subscription.basket.line',

inverse_name='basket_id',

domain=[('data_type','=','verduras')]

)

temperos = fields.One2many(

string=u'Temperos',

comodel_name='subscription.basket.line',

inverse_name='basket_id',

domain=[('data_type','=','temperos')]

)

and in view.xml maybe you can pass a default field context

for example

<field name="temperos" context="{'default_data_type':'temperos'}"/>

Regards

La Jayuhni Yarsyah

Автор

I worked just as I wanted, thank you! If you past this last comment on an answer bellow, I will be glad to mark it

Найкраща відповідь

Hello, you are from Brazil, right? 

I to hire someone with Odoo Experience, are you interested? 

Аватар
Відмінити
Related Posts Відповіді Переглядів Дія
1
вер. 17
4294
0
квіт. 16
9
0
лют. 21
3776
1
квіт. 18
6079
1
січ. 25
1595