在odoo中,可以根据用户选择的月份来获取月份的开始日期和结束日期。具体实现方法如下:
class MonthSelection(models.Model):
_name = 'month.selection'
month = fields.Selection([
('1', 'January'),
('2', 'February'),
('3', 'March'),
('4', 'April'),
('5', 'May'),
('6', 'June'),
('7', 'July'),
('8', 'August'),
('9', 'September'),
('10', 'October'),
('11', 'November'),
('12', 'December')
], string='Month', required=True)
class MonthSelection(models.Model):
_name = 'month.selection'
month = fields.Selection([
('1', 'January'),
('2', 'February'),
('3', 'March'),
('4', 'April'),
('5', 'May'),
('6', 'June'),
('7', 'July'),
('8', 'August'),
('9', 'September'),
('10', 'October'),
('11', 'November'),
('12', 'December')
], string='Month', required=True)
start_date = fields.Date(string='Start Date', compute='_compute_dates')
end_date = fields.Date(string='End Date', compute='_compute_dates')
@api.depends('month')
def _compute_dates(self):
for record in self:
year = datetime.now().year
month = int(record.month)
start_date = datetime(year, month, 1).date()
end_date = (datetime(year, month, calendar.monthrange(year, month)[1])
.date())
record.start_date = start_date
record.end_date = end_date
<record id="view_month_selection_form" model="ir.ui.view">
<field name="name">month.selection.form</field>
<field name="model">month.selection</field>
<field name="arch" type="xml">
<form string="Month Selection">
<sheet>
<group>
<field name="month"/>
<field name="start_date"/>
<field name="end_date"/>
</group>
</sheet>
</form>
</field>
</record>
<record id="action_month_selection" model="ir.actions.act_window">
<field name="name">Month Selection</field>
<field name="res_model">month.selection</field>
<field name="view_mode">form</field>
<field name="view_id" ref="view_month_selection_form"/>
</record>
<menuitem id="menu_month_selection" name="Month Selection" parent="base.menu_sales"
action="action_month_selection"/>
以上是根据用户在odoo中选择的月份来获取月份的开始日期和结束日期的实现方法。在odoo中,可以通过创建模型、计算字段和视图来实现这一功能。
领取专属 10元无门槛券
手把手带您无忧上云