正如標題所示, 角色和位置的模型都包含數據,因爲它插入管理頁面並進行確認。 如果它被註釋掉,我會得到與位置相同的問題而不是角色。Django,角色匹配查詢不存在
DoesNotExist at /api/add/res/
Role matching query does not exist.
Traceback:
File "/usr/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response 132.
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/speedy/hrdb/hrdb/api.py" in set 26.
role=Role.objects.get(role_name=data['role']),
File "/usr/lib/python2.7/site-packages/django/db/models/manager.py" in manager_method 127.
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/usr/lib/python2.7/site-packages/django/db/models/query.py" in get 334.
self.model._meta.object_name
這裏是API
def set(request):
data = json.loads(request.body)
res = Resource.objects.get_or_create(
title=data['title'],
preferred_name=data['preferred_name'],
last_name=data['last_name'],
employstatus=data['employstatus'],
employer=data['employer'],
role=Role.objects.get(role_name=data['role']),
location=Location.objects.get(name=data['location']),
workphone=data['workphone'],
mobile_phone=data['mobile_phone'],
email=data['email'],
notes=data['notes'],
updated_by=data['updated_by'],
)
print res
return HttpResponse('"Submitted"', content_type='application/json')
這裏是示範
class Resource(models.Model):
title = models.CharField(max_length=10)
preferred_name = models.CharField(max_length=20)
last_name = models.CharField(max_length=30)
employstatus = models.CharField(max_length=20)
employer = models.CharField(max_length=30)
role = models.ForeignKey('Role')
location = models.ForeignKey('Location')
workphone = models.CharField(max_length=25, blank=True, null=True)
mobile_phone = models.CharField(max_length=15, blank=True, null=True)
email = models.CharField(max_length=15, blank=True, null=True)
notes = models.CharField(max_length=200, blank=True, null=True)
updated_by = models.CharField(max_length=30, blank=True, null=True)
class Location(models.Model):
name = models.CharField(max_length=200)
phone_number = models.CharField(max_length=200)
class Role(models.Model):
role_name = models.CharField(max_length=200)
role_description = models.CharField(max_length=200)
廣告,這是JS的東西
var data={}
$.each($('#addPersonnel').find('.form-control'), function(){
var field = this.id,
value = $(this).val()
data[field] = value
})
//data from add resource model to database with success function
$.ajax({
type: "POST",
url: "/api/add/res/",
data: JSON.stringify(data),
success: function(response) {
var successMessage = $('<div>').text('Successfully saved to database...').css('color', 'green');
$('.form-group').removeClass('has-error')
$('#submitresource').html('Added!').addClass('btn-success')
$('.modal-body').append(successMessage);
window.setTimeout(function() {
$('#addPersonnel').modal('hide'); }, 1000);
console.log("yers")
},
contentType: "application/json",
dataType: "json"
這裏是一些呢。這是HTML,相關的位置和角色
<div class="form-group">
<label for="role" class="col-lg-3 control-label">Role</label>
<div class="col-lg-8">
<select class="form-control" id="role">
</select>
</div>
</div>
<div class="form-group">
<label for="location" class="col-lg-3 control-label">Location</label>
<div class="col-lg-8">
<select class="form-control" id="location">
</select>
</div>
</div>
的一部分,這是下拉JS的東西(注僱主工作正常)
var data={}
$(document).ready(function() {
$.get('/api/new/emp/', function(response){
$('#employer').empty()
$('#location').empty()
$('#role').empty()
$.each(response.Employers, function(){
$('#employer').append('<option value='+this+'>'+this+'</option>')
})
$.each(response.Locations, function(){
$('#location').append('<option value='+this+'>'+this+'</option>')
})
$.each(response.Roles, function(){
$('#role').append('<option value='+this+'>'+this+'</option>')
這是API的東西。
def addresddpop(request):
data = {
'Employers':[],
'Locations':[],
'Roles':[]
}
# Get a list of all employers
for ddemp in Employer.objects.all():
data['Employers'].append(ddemp.employer_name)
# Get a list of all locations
for ddloc in Location.objects.all():
data['Locations'].append(ddloc.name)
# Get a list of all roles
for ddrol in Role.objects.all():
data['Roles'].append(ddrol.role_name)
return HttpResponse(json.dumps(data), content_type='application/json')
什麼是張貼的價值'數據[ '角色']'? 「角色」模型? – souldeux
包括角色和位置模型 – SpeedyH30
數據通過下拉選擇框提供 – SpeedyH30