一、
ModelChoiceField(ChoiceField) ... django.forms.models.ModelChoiceField queryset, # 查询数据库中的数据 empty_label="---------", # 默认空显示内容 to_field_name=None, # HTML中value的值对应的字段 limit_choices_to=None # ModelForm中对queryset二次筛选ModelChoiceField要配合models使用:
def __str__(self):
return self.name
二、
def __init__(self, *args, **kwargs):
super(RecordForm, self).__init__(*args, **kwargs)
self.fields['machine_room'].widget.choices = MachineRoom.objects.all().values_list('machine_room_id','machine_room_name')
self.fields['man'].widget.choices = UserInfo.objects.all().values_list('user_id', 'user_name')
以上2中方式都在form中实现