Empty Label ChoiceField Django

后端 未结 7 936
猫巷女王i
猫巷女王i 2020-12-23 19:38

How do you make ChoiceField\'s label behave like ModelChoiceField? Is there a way to set an empty_label, or at least show a blank fiel

7条回答
  •  旧时难觅i
    2020-12-23 20:34

    Had to use 0 instead of u'' because of integer field in model. (Error was invalid literal for int() with base 10: ')

    prepend an empty label if it exists (and field is not required!)

        if not required and empty_label is not None:
            choices = tuple([(0, empty_label)] + list(choices))
    

提交回复
热议问题