我想在ModelForm中的Model的ForeignKeyField上设置select选项。以下是我的模型和表格。我只想在我的ModelForm中为value=1
字段设置预选的State
。因此它向用户显示为已选择的选项。
Customer Model:
class Customer(models.Model):
"""
Customer model
"""
GENDER_CHOICES = (('M', 'M'),
('F', 'F'))
SDW_CHOICES = (('S/o', 'S/o'),
('D/o', 'D/o'),
('W/o', 'W/o'))
name = models.CharField(verbose_name='customer name', max_length=50,
help_text='max length 50 characters')
guardian_name = models.CharField(verbose_name='customer guardian name', max_length=50,
help_text='max length 50 characters')
phone_no_1 = models.CharField(verbose_name='customer phone number 1', max_length=10, null=True, blank=True,
help_text='max length 10 digits')
phone_no_2 = models.CharField(verbose_name='customer phone number 2', max_length=10, null=True, blank=True,
help_text='max length 10 digits')
address = models.CharField(verbose_name='customer address', max_length=150,
help_text='max length 150 characters')
state = models.ForeignKey(State, help_text='Please select from list')
district = models.ForeignKey(District, help_text='Please select from list')
village_or_city = models.ForeignKey(VillageOrCity, help_text='Please select from list')
created_on = models.DateTimeField(verbose_name='customer added on', auto_now_add=True)
updated_on = models.DateTimeField(verbose_name='customer last modified', auto_now=True)
remark = models.TextField(max_length=150, null=True, blank=True, help_text='max length 150 characters')
gender = models.CharField(max_length=1, choices=GENDER_CHOICES, help_text='Please select from list')
image = models.ImageField(upload_to=MEDIA_ROOT, default=DEFAULT_USER_IMAGE)
sdw = models.CharField(verbose_name='Son/daughter/wife of', max_length=3, choices=SDW_CHOICES,
help_text='Please select from list')
caste = models.ForeignKey(Caste, help_text='Please select from list')
created_by = models.ForeignKey(User, related_name='created_by', null=True)
updated_by = models.ForeignKey(User, related_name='updated_by', null=True)
CreateCustomerForm:
class CreateCustomerForm(forms.ModelForm):
"""
CreateCustomerForm class. Renders Customer model form
"""
class Meta:
model = Customer
fields = ('name', 'sdw', 'guardian_name', 'phone_no_1',
'phone_no_2', 'address', 'state', 'district',
'village_or_city', 'remark', 'gender', 'caste',
'image'
)
widgets = {
'name': forms.TextInput(attrs={'placeholder': 'Enter customer name',
'required': 'required',
'autofocus': 'autofocus', 'size': "50"
}
),
'sdw': forms.Select(attrs={'required': 'required',
}
),
'guardian_name': forms.TextInput(attrs={'placeholder': 'Enter guardian name',
'required': 'required', 'size': "50"
}
),
'phone_no_1': forms.TextInput(attrs={'placeholder': 'Enter phone number',
'size': "50"
}
),
'phone_no_2': forms.TextInput(attrs={'placeholder': 'Enter phone number',
'size': "50"
}
),
'address': forms.TextInput(attrs={'placeholder': 'Enter address',
'required': 'required', 'size': "50"
}
),
'state': forms.Select(attrs={'required': 'required',
}
),
'district': forms.Select(attrs={'required': 'required',
}
),
'village_or_city': forms.Select(attrs={'required': 'required',
}
),
'remark': forms.Textarea(attrs={'placeholder': 'Enter remark',
}
),
'gender': forms.Select(attrs={'required': 'required',
}
),
'caste': forms.Select(attrs={'required': 'required',
}
),
}
答案 0 :(得分:2)
加载表单时,您可以为initial
设置ChoiceFields
值,这样就可以了
default_state = State.objects.get(id=1)
form = CreateCustomerForm(initial={'state': default_state})
请查看Setting the selected value on a Django forms.ChoiceField以了解设置默认值的其他方法
答案 1 :(得分:1)
使用initial
在Django的CBV中设置初始数据。
class CreateCustomerView(LoginRequiredMixin, CreateView):
"""
Handles request for '/add/customer/'
"""
initial = {'state': '1'
}
form_class = CreateCustomerForm
template_name = CREATE_CUSTOMER_TEMPLATE
它将使用预先填充的数据呈现模板。