Django: Overriding __init__ for Custom Forms

前端 未结 2 452
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-29 04:58

I am making a custom form object in Django which has an overrided __init__ method. The purpose of overriding the method is to dynamically generate drop-down boxes based on t

2条回答
  •  伪装坚强ぢ
    2020-12-29 05:25

    You can dynamically modify your form by using the self.fields dict. Something like this may work for you:

    class TicketForm(forms.Form):
    
      Type = Type.GetTicketTypeField()
    
      def __init__(self, ticket, *args, **kwargs):
        super(TicketForm, self).__init__(*args, **kwargs)
        self.fields['state'] = State.GetTicketStateField(ticket.Type)
    

提交回复
热议问题