コンテンツへスキップ
メニュー
この質問にフラグが付けられました
3 返信
109 ビュー

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?

アバター
破棄
最善の回答

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.

アバター
破棄
最善の回答

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.

アバター
破棄
著作者 最善の回答

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.

アバター
破棄
関連投稿 返信 ビュー 活動
1
6月 25
1332
1
5月 25
1625
13
9月 25
27395
3
9月 25
1190
1
7月 25
703