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

In the odoo event.booth.configurator, which pops up when selecting event booth product in a sale order, you have to select the event and the booth type. However, in my company, I always only have one event at a time, all others are either not created yet or archived. So, when i open the event configurator, I want to check whether there is only one Event within this company that is active (aka not archived) and then automatically select it.

Here is my current attempt:

# -*- coding: utf-8 -*-
from odoo import models, fields, api
# from . import prepare_engine as rectanglemaps_engine


class EventBoothConfigurator(models.TransientModel):
_inherit = 'event.booth.configurator'

# plan = fields.Binary(string='Plan', attachment=True)

@api.model
def create(self, values):

# If the company only has one event active, we set it by default
if not values.get('event_id'):
values['event_id'] = self.env['event.event'].search([('active', '=', True)], limit=1).id


return super(EventBoothConfigurator, self).create(values)

I do not get an error message, however, nothing happens

How can I make this possible? 

Is the create method the right idea?



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

Hi Josef,

You can use the default_get function for this.  Pass your default event_id as below:

@api.model
def default_get
(self, fields):
res = super(EventBoothConfigurator, self).default_get(fields)
events = self.env['event.event'].search([])
if len(events) == 1:
res['event_id'] = events.id
return res

For more details on default_get function refer thisDefault Get Function in Odoo || Set Default Values Using Default Get Function

Hope this will help you

Thank you

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

well that was an amazing tip! Thanks for letting me know!
I do have one more question: Does this ensure that the selected event belongs to the company that is currently in use? I use multicompany in my odooinstance, so this has to work.
Or do I have to add something like search([('company', '=', active_company)]

?

If you have multi company instance you have to pass the company domain also in the search
Thank you

Tác giả

@Mehjabin Farsana thank you, can you tell me how?

pass your company domain as this
events = self.env['event.event'].search([('company_id', '=', self.env.company.id)])

Bài viết liên quan Trả lời Lượt xem Hoạt động
0
thg 4 23
1852
4
thg 4 25
4421
1
thg 7 22
6604
1
thg 8 25
3589
1
thg 6 23
5268