Skip to Content
เมนู
คุณต้องลงทะเบียนเพื่อโต้ตอบกับคอมมูนิตี้
คำถามนี้ถูกตั้งค่าสถานะ
2 ตอบกลับ
12616 มุมมอง

I have a selection field and this function for list items in field by dynamically.

def get_years(self):
        year_list = []
        year = date.today().year+1
        lastyear = date.today().year - 20
        for i in range(int(lastyear),int(year)):
            year_list.append((i, str(i)))
        return year_list

this is the field,

year = fields.Selection(get_years, string='Yıl', default=get_current_year, restore="True")

and when i save the form, this field become unknown. I found what is the problem but dont know the solution. Problem is compute function, when i write items like [(1997,1997),(2016,2016),(2017,2017)] it is working but i dont want to write hard code. How can i solve this problem. Thank you.

อวตาร
ละทิ้ง
คำตอบที่ดีที่สุด

Hello Serdar Eren,

I Check your code, as per your scenario no need to create any method for that.

it is possible within the field.

Show below code:

year = fields.Selection([(num, str(num)) for num in range(1900, (datetime.now().year)+1 )],

             string='Year', default=datetime.now().year)

It works fine. I hope this will help you.

Thanks

อวตาร
ละทิ้ง
ผู้เขียน คำตอบที่ดีที่สุด

Thank you so much, it works fine.

Have a good day..

อวตาร
ละทิ้ง

Welcome, Serdar Eren :)

Related Posts ตอบกลับ มุมมอง กิจกรรม
1
เม.ย. 25
1024
Selection Field, how does it work? แก้ไขแล้ว
2
เม.ย. 15
30270
1
พ.ค. 23
3126
5
พ.ค. 22
25649
0
พ.ค. 18
5979