from django import forms
    from wang import models
    class ClothesForm(forms.Form):
        color = forms.IntegerField(required=True, widget=forms.Select(), )
        def __init__(self, *args, **kwargs):
            # 定义这个关键字段,当使用form时,colors表新增了颜色,前端ClothesForm的color字段的选项会自动更新
            super(ClothesForm, self).__init__(*args, **kwargs)
            self.fields['color'].widget.choices = Colors.objects.all().order_by('id').values_list('id', 'colors')

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-05-21
  • 2021-06-09
  • 2021-11-17
  • 2022-03-10
  • 2022-02-04
猜你喜欢
  • 2021-10-06
  • 2021-10-20
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案