Bỏ qua để đến Nội dung
Menu
Câu hỏi này đã bị gắn cờ
1 Trả lời
3817 Lượt xem

I have 2 classes and with Many2one-relation. How can i display  the title from class A in a form view from class B (title_from_a)?

The form view for class B does not show the title, but after saving it shows the title. is there a solution?


class A(models.Model):

    _name = 'class.a'

    title = fields.Char('Title')

    b_ids = fields.One2Many('class.b', 'a_id')


class B(models.Model):

    _name = 'class.b'

    a_id = fields.Many2one('class.a')

    title_from_a = fields.Char(related='a_id.title')


Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

Hello ai,

You can give _rec_name for display name in models.

For this, try below code:


class B(models.Model):

    _name = 'class.b'

    _rec_name = 'title_from_a'


    a_id = fields.Many2one('class.a')

    title_from_a = fields.Char(related='a_id.title')


Hope it will works for you.

Thanks,

Ảnh đại diện
Huỷ bỏ
Tác giả

thanks for your answer, but I would like to have that value for working with it. Is that possible?

When you saved record then title_from_a is display

Tác giả

Yes, but I want to display in a form before and not after saving. is that possible?

If you want to display as display name then it is not possible. if you want to display in form view then select a_id and after selecting a_id automatic display it's title in title_form_a field.

Tác giả

I just want to display in form view of class.b. but title_from_a does not appear on the form view.

Try to give _rec_name to class a.

For Ex:

class A(models.Model):

_name = 'class.a'

_rec_name = 'title'

title = fields.Char('Title')

b_ids = fields.One2Many('class.b', 'a_id')

Tác giả

it does not work. title_from_a is still empty. ;(

Bài viết liên quan Trả lời Lượt xem Hoạt động
0
thg 11 18
3001
0
thg 7 21
3121
2
thg 11 24
15232
3
thg 7 22
7852
1
thg 5 24
3180