Skip to Content
Menu
This question has been flagged
2 Replies
125 Views

I'm using Odoo 18. I have the following models:

  • document.foo

class FooDocument(models.Model):

  _name = 'document.foo'

  _description = 'Foo'


  date = fields.Datetime(

    string='Date',

  )

  number = fields.Char(

    string='Number',

  )

  • document.bar

class BarDocument(models.Model):

  _name = 'document.bar'

  _description = 'Bar'


  date = fields.Datetime(

    string='Date',

  )

  number = fields.Char(

    string='Number',

  )

  • document.invoice
class InvoiceDocument(models.Model):
  _name = 'document.invoice'
  _description = 'Invoice'

  date = fields.Datetime(
    string='Date',
  )
  number = fields.Char(
    string='Number',
  )
  doc_base_name = fields.Selection(
    string='Base Document Type',
    selection=[
      ('document.foo', 'Foo'),
      ('document.bar', 'Bar'),
    ],
    default='document.foo',
  )
  doc_base_id = fields.Many2oneReference(
    string='Base Document',
    model_field='doc_base_name',
  )

Also, I have the following view for document.invoice​​

<record id="document_invoice_view_form" model="ir.ui.view">

<field name="name">document.invoice.form</field>

<field name="model">document.invoice</field>

<field name="arch" type="xml">

<form>

<sheet>

<group>

<field name="date" />

<field name="number" />

<field name="doc_base_name" />

<field name="doc_base_id" />

</group>

</sheet>

</form>

</field>

</record>

Then, I am doing the following actions in the Odoo web interface:

1.Creating new records of document.foo​​

2.Creating a new record of document.invoice

*Choosing Foo value in the doc_base_name (Base Document Type) field

*Trying to choose a value in the doc_base_id (Base Document) field, but there are no values. name_search​ method doesn't return values

How to solve this problem with doc_base_id (Many2oneReference) field?

Avatar
Discard
Author

Thank you for your answers! It turned out the reason was the old version of my 18.0 branch from Github. The problem disappeared after pulling current version.

Best Answer

Your example source works perfectly fine in terms of having the Many2oneReference field be a (M2o-)selection of Foo or Bar:


The name_search in fact would in your case return always all values - because there just isn't any definition of what an entered name should be searched against (since there's no name field).

If you would, for example, set a _rec_name on the Foo and Bar models (or define an name, or a computed display_name), i.e.

class DocumentFoo(models.Model):
    _name = 'document.foo'
    _description = 'Foo'
    _rec_name = 'number'

    date = fields.Datetime(string='Date', )
    number = fields.Char(string='Number', )

 

also your name_search would actually return a 'filtered' result.

Avatar
Discard
Best Answer

Hi,

You need to give document.foo and document.bar models a way to represent themselves.


If you want to use the existing number field as the record name:


class FooDocument(models.Model):

    _name = 'document.foo'

    _description = 'Foo'

    _rec_name = 'number' # <--- Add this


    date = fields.Datetime(string='Date')

    number = fields.Char(string='Number')



class BarDocument(models.Model):

    _name = 'document.bar'

    _description = 'Bar'

    _rec_name = 'number' # <--- Add this


    date = fields.Datetime(string='Date')

    number = fields.Char(string='Number')



This way, when you open the dropdown for doc_base_id, Odoo will display the number values of Foo/Bar records.


Hope it helps.

Avatar
Discard
Related Posts Replies Views Activity
1
Jun 25
1339
1
May 25
1641
13
Sep 25
27442
3
Sep 25
1198
1
Jul 25
708