Skip to Content
Menu
Musisz się zarejestrować, aby móc wchodzić w interakcje z tą społecznością.
To pytanie dostało ostrzeżenie
4 Odpowiedzi
2625 Widoki

​I am working on an app where I need to set a default pricelist, based on whether the res.partner (Contact) is an individual or a company. This needs to be a field in the settings tab, so that the user can select which pricelist to use for company and which one for individuals. I managed to get this field to show up with this model:
class ResConfigSettings(models.TransientModel):
_inherit = 'res.config.settings'

​ default_pricelist_company = fields.Many2one(
        ​'product.pricelist',
        ​string="Default Pricelist for Companies",
        ​default_model='res.partner',
        ​config_parameter='my_addon.default_pricelist_company'
​) 
default_pricelist_individual = fields.Many2one(
        ​'product.pricelist',
        string="Default Pricelist for Individuals",
        ​default_model='res.partner',
        config_parameter='my_addon.default_pricelist_individual' 
​)

However, this causes an error on save, that the field res.partner.pricelist_company does not exist.
I am relatively new to odoo so I am not sure exactly what i am doing wrong or how to properly set up these settings. Any help, or explanation of what i am doing wrong is highly appreciated, thanks!

Awatar
Odrzuć
Autor

I tried removing the default_model, but then I ran into this error:

File "/home/linuxsub/odoo/odoo/odoo/addons/base/models/res_config.py", line 427, in _get_classified_fields
raise Exception("Field %s without attribute 'default_model'" % field)
Exception: Field res.config.settings.default_pricelist_company without attribute 'default_model'

Najlepsza odpowiedź

Hi,

The default_model argument is unnecessary here, as the settings are stored in the system parameter, not directly on the res.partner model. so default_model should be removed

Hope this will help you

thanks


Awatar
Odrzuć
Autor Najlepsza odpowiedź

The problem I was encountering was because my variables started with "default_", changing them resolved the issue in my case.

Awatar
Odrzuć
Najlepsza odpowiedź

Hello Daniel, I'll help you set up the Many2one fields in res.config.settings correctly.



  The error occurs because Odoo attempts to write the value of default_pricelist_company and default_pricelist_individual directly to a field named pricelist_company and pricelist_individual on the res.partner model, which do not exist.

  Remove the default_model='res.partner' from your field definitions. This attribute is not needed and causes Odoo to look for corresponding fields on res.partner.

  Instead of relying on default_model, you will need to create a function that applies the pricelist based on the configuration when a partner is created or updated. This can be achieved by inheriting the create and write methods of res.partner.

  Here's an example of how you might structure your res.config.settings model:
  ```python
  class ResConfigSettings(models.TransientModel):
    _inherit = 'res.config.settings'

    default_pricelist_company = fields.Many2one(
        'product.pricelist',
        string="Default Pricelist for Companies",
        config_parameter='my_addon.default_pricelist_company'
    )
    default_pricelist_individual = fields.Many2one(
        'product.pricelist',
        string="Default Pricelist for Individuals",
        config_parameter='my_addon.default_pricelist_individual'
    )
  ```

  Then, in your res.partner model, override the create and write methods to apply the pricelists:
  ```python
  class ResPartner(models.Model):
    _inherit = 'res.partner'

    @api.model
    def create(self, vals):
        partner = super(ResPartner, self).create(vals)
        self._set_default_pricelist(partner)
     &

Awatar
Odrzuć
Najlepsza odpowiedź

Change your field  name from default_pricelist_company to pricelist_company_default

Because if you set the first name of your field name by default, it will raise this error


Awatar
Odrzuć
Powiązane posty Odpowiedzi Widoki Czynność
2
wrz 25
1723
4
wrz 25
6782
0
sie 25
816
4
maj 25
3428
2
maj 25
6799