I'm trying to create a model field for months that:
- Displays month names in a selection dropdown
- Stores the month as an integer value
- Ensures the integer is preserved during export/import
Here's my current approach:
pythonCopyclass MyModel(models.Model):
_name = 'my.model'
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')
month_int = fields.Integer(
string='Month Number',
compute='_compute_month_int',
store=True
)
@api.depends('month')
def _compute_month_int(self):
for record in self:
record.month_int = int(record.month) if record.month else FalseQuestions:
- Is this the correct way to create a month selection field that stores as an integer?
- Are there any potential issues with export/import using this method?
- Would you recommend any improvements to this approach?
I want to ensure:
- User-friendly month selection
- Accurate integer storage
- Compatibility with exports/imports
Any guidance or best practices would be greatly appreciated!