简体   繁体   中英

Use Selection Field in Another Model in Odoo 13

I'm totally a newbie here who love Odoo so much:)

I got stuck with this problem for a few days in Odoo CE 13.

I want my SHIPPING TYPE field which I made in sale.order can be used in purchase.order as well.

So I did this in shipping_type.py

  # -*- coding: utf-8 -*-
    from odoo import models, fields
    
    class SaleOrder(models.Model):
        _inherit = 'sale.order'
    
        shipping_selection = [
                    ('type1', 'Instant'),
                    ('type2', 'Same Day'),
                    ('type3', 'JNE'),
                    ('type4', 'Tokopedia'),
                    ('type5', 'Pick Up'),
                    ('type6', 'AnterAja-Reguler'),
                    ('type7', 'J&T-Reguler'),
                    ('type8', 'Ninja-Reguler'),
    
                     ]
    
        shipping_type = fields.Selection(shipping_selection,'Shipping Type',)

Then, I was trying to make purchase_shipping_type.py in another addons, and created this:

# -*- coding: utf-8 -*-
from odoo import models, fields

class SaleOrder(models.Model):
    _inherit = ['sale.order']

    shipping_selection = [
                ('type1', 'Instant'),
                ('type2', 'Same Day'),
                ('type3', 'JNE'),
                ('type4', 'Tokopedia'),
                ('type5', 'Pick Up'),
                ('type6', 'AnterAja-Reguler'),
                ('type7', 'J&T-Reguler'),
                ('type8', 'Ninja-Reguler'),

                 ]

    shipping_type = fields.Selection(shipping_selection,'Shipping Type',)

class PurchaseOrder(models.Model):
    _inherit = ['purchase.order']

   shipping_type_purchase = fields.Selection(string='Shipping Type',related=shipping_type.shipping_type_purhase, readonly=True)

This error showed up:

Aug 21 06:29:03 kama-odoo-server odoo13[24202]: NameError: name 'shipping_type' is not defined - - -

How to set up this properly? I really hope someone can help me:) Thanks!

The related attribute is used to specify a sequence of field names.

The value of a related field is given by following a sequence of relational fields and reading a field on the reached model. The complete sequence of fields to traverse is specified by the related attribute.

When using Selction fields, The attribute selection is mandatory except in the case of related or extended fields.

You can find a related selection field in account model which is declared as following:

user_type_id = fields.Many2one('account.account.type', ...)
internal_type = fields.Selection(related='user_type_id.type', ...)

You can find also a selection field in account partner which uses a constant list declared in base res_partner model:

from odoo.addons.base.models.res_partner import WARNING_MESSAGE, WARNING_HELP

invoice_warn = fields.Selection(WARNING_MESSAGE, 'Invoice', help=WARNING_HELP, default="no-message")

To keep the declaration of the shipping_selection in SaleOrder class, you need to remove related attribute and modify the shipping_type_purchase field as followin:

shipping_type_purchase = fields.Selection(shipping_selection, ...)

If SaleOrder class is not declared in the same module, you have just to import it like in the example above.

You can decalre the shipping_selection as a constant list then modify the shipping_type_purchase field:

SHIPPING_SELECTION = [
        ('type1', 'Instant'),
        ('type2', 'Same Day'),
        ('type3', 'JNE'),
        ('type4', 'Tokopedia'),
        ('type5', 'Pick Up'),
        ('type6', 'AnterAja-Reguler'),
        ('type7', 'J&T-Reguler'),
        ('type8', 'Ninja-Reguler'),

    ]

class SaleOrder(models.Model):

    shipping_type = fields.Selection(SHIPPING_SELECTION, ...)


class PurchaseOrder(models.Model):
    
    shipping_type_purchase = fields.Selection(SHIPPING_SELECTION, ...)

Replace shipping_type in PurchaseOrder with SaleOrder.shipping_type and it should do it.

The reason is that shipping_type is defined as part of a class and should be referenced as such

[EDIT]: In this particular case though you have a relation between your models and it should be handled with a one2many or many2one field (see here )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM