[英]Select all of django foreign key model if condition or part if another condition
[英]Django Form Select From another model without Foreign Key
我想讓 th_wali 從另一種模式中獲取價值。 在 User_Admin model 中沒有“th_wali”
class InsertUser(forms.ModelForm):
class Meta():
model = User_Admin
fields = '__all__'
labels ={
'username': 'Username',
'password': 'Password',
'role' : 'Role',
'th_wali' : 'Tahun Wali',
}
widgets = {
'username':forms.TextInput(
attrs={
'class':'form-control',
'placeholder':'username',
'required' : True,
'minlength' : 8,
}
),
'th_wali':forms.ModelChoiceField(queryset=Mhs_SI.objects.values('th_masuk').distinct()),
}
我像這樣調用模板,但沒有出現
<div class="form-group row">
<label class="control-label col-md-3 col-sm-3 ">{{formUser.th_wali.label_tag}}</label>
<div class="col-md-9 col-sm-9 ">
{{formUser.th_wali}}
</div>
</div>
您可以使用額外的字段擴展ModelForm
。
class InsertUser(forms.ModelForm):
class Meta():
model = User_Admin
fields = ('username', 'password', 'role', )
labels ={
'username': 'Username',
'password': 'Password',
'role' : 'Role',
}
widgets = {
'username':forms.TextInput(
attrs={
'class':'form-control',
'placeholder':'username',
'required' : True,
'minlength' : 8,
}
),
}
現在您可以通過擴展ModelForm
使用額外字段th_wali
擴展此InsertUser
class ThUser(InsertUser):
th_wali = forms.ModelChoiceField(queryset=Mhs_SI.objects.values('th_masuk').distinct())
class Meta(InsertUser.Meta):
fields = InsertUser.Meta.fields + ('th_wali')
現在您可以使用ThUser
表單訪問th_wali
。
{{ThUser.th_wali}}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.