總結:怎樣填充預渲染的Django ChoiceField/ModelChoiceField動態使用角時http服務從服務器檢索數據。
我無法用函數返回的數據填充Django ModelChoiceField。我確實有角模板的工作,但問題是,我將無法驗證這些字段與form.is_valid()保存到數據庫之前,因爲我已經硬編碼<select>
字段到我的HTML。
我有一個ModelChoiceField的原因是因爲我試圖過濾notification_type的值並將它們附加到notification_type字段。即使表單字段已成功填充值,但我無法返回數據並在我的模板中顯示,因爲我有一個角度控制器期待數據。
守則
HTML
<div>
<label>Select department</label>
<div>
{{ form.department }}
</div>
</div>
<div>
<label>Select notification type</label>
<div ng-controller="selectNotificationTypeCtlr">
<select ng-change="getNotfications()" ng-model="selectedNotificationType">
<option ng-repeat="i in notificationTypes" value="{[{ i.pk }]}">{[{ i.fields.notification_type }]}</option> <!-- fields.notification_type -->
</select>
</div>
</div>
app.js
selectNotification.controller('selectDepartmentCtlr', function($scope, $http) {
$scope.getNotificationType = function() {
var id = $scope.selectedDepartment;
var notificationTypes = [];
$http({
method : "GET",
url : id
}).then(function success(response) {
$scope.notificationTypes = response.data;
}, function error(response) {
var errorMessage = "Oops it looks like something went wrong. Please try again.";
});
};
});
正如你可以看到我瞭解的過程,它只是與Django表單它整合。
views.py
@login_required(login_url="/login/")
def get_notification_types(request, id):
if request.method == "GET":
department = Department.objects.get(id=id)
notification_types = NotificationType.objects.filter(department=department.id)
return HttpResponse(serializers.serialize('json', notification_types))
forms.py
from django import forms
from notifications.models import Department
from notifications.models import NotificationType
class SelectNotificationForm(forms.Form):
department = forms.ModelChoiceField(queryset=Department.objects.all(), widget=forms.Select(attrs={'class':'input-element', 'ng-change':'getNotificationType()', 'ng-model':'selectedDepartment'})) #widget=forms.Select(attrs={'class': 'add_class_here'}) # ChoiceField(widget=forms.Select()
notification_type = forms.ModelChoiceField(queryset=NotificationType.objects.none(), widget=forms.Select(attrs={'class':'input-element'}))
notification_name = forms.ChoiceField(widget=forms.Select(attrs={'class':'input-element'}))
notification_contacts = forms.MultipleChoiceField(widget=forms.SelectMultiple(attrs={'class':'input-element'}))
subject = forms.CharField(widget=forms.Select(attrs={'class':'input-element'}))
body = forms.CharField(widget=forms.Textarea)
它是如何顯示
下面是該網站與我當前的代碼運行。
的援助之手,我需要
我想使用Django,當它被保存到通過is_valid()函數驗證我的形式,但我也需要填充第二ChoiceField將JSON數據對象返回給我的Angular Controller。真正令我感到困擾的是,它和Angular一起工作良好,只是我需要使用{{form.notification_type}}來阻止我循環顯示我的Angular控制器返回的JSON對象。
HTML模板,讓我來驗證ChoiceField/ModelChoiceFields
HTML(理想情況下,我想呈現的形式按下面的代碼,這樣我就可以驗證表單。)
<div>
<label>Select department</label>
<div>
{{ form.department }}
</div>
</div>
<div>
<label>Select notification type</label>
<div ng-controller="selectNotificationTypeCtlr">
{{ form.notification_type }}
</div>
</div>
我意識到一個解決方法,它將隨後意味着我在所有形式的ChoiceFields上設置required = False。不過,我覺得這可能會導致更多問題。
任何意見或幫助將不勝感激。