Setting initial Django form field value in the __init__ method

前端 未结 6 642
余生分开走
余生分开走 2020-12-13 08:55

Django 1.6

I have a working block of code in a Django form class as shown below. The data set from which I\'m building the form field list can include an initial val

6条回答
  •  南方客
    南方客 (楼主)
    2020-12-13 09:48

    I had that exact same problem and I solved it doing this:

    def __init__(self, *args, **kwargs):
        instance = kwargs.get('instance', None)
    
        kwargs.update(initial={
            # 'field': 'value'
            'km_partida': '1020'
        })
    
        super(ViagemForm, self).__init__(*args, **kwargs)
    
        # all other stuff
    

提交回复
热议问题