2015-12-03 58 views
0

正如標題所示, 角色和位置的模型都包含數據,因爲它插入管理頁面並進行確認。 如果它被註釋掉,我會得到與位置相同的問題而不是角色。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') 
+1

什麼是張貼的價值'數據[ '角色']'? 「角色」模型? – souldeux

+0

包括角色和位置模型 – SpeedyH30

+0

數據通過下拉選擇框提供 – SpeedyH30

回答

0

這是好的世界我解決了這個問題。 因爲我在我的角色和位置空間的下拉菜單是這個問題,使我改變了他們有點

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>') 
      $('#employer').append($('<option></option>').val(this).text(this)) 
    }) 

     $.each(response.Locations, function(){ 
      //$('#location').append('<option value='+this+'>'+this+'</option>') 
      $('#location').append($('<option></option>').val(this).text(this)) 
    }) 

     $.each(response.Roles, function(){ 
      // $('#role').append('<option value='+this+'>'+this+'</option>') 
      $('#role').append($('<option></option>').val(this).text(this)) 

我已經離開了原來的代碼在,所以你可以看到修復

1

我認爲有兩件事情中的一件正在發生。可能性一:您發佈的RoleLocation的值與您所期望的格式不符。例如,下拉列表中的標籤可能是namerole_name,但發佈的值可能是ID。您提到這些數據點是通過下拉菜單選擇的 - 請仔細檢查您的optionvalue屬性select以確保它們確實是名稱而不是ID。

或者,您的jquery功能在設置選擇之前設置data['role_name']data['location'](或者如果選擇被更改,則無法更改值)。

如果您發佈的價值爲data我們可以找出哪一個是這種情況,但我幾乎肯定它是這兩個問題之一。

+0

它的奇怪,因爲當你檢查params中的有效載荷時,我輸入的所有數據都在那裏。 {「title」:「Mr」,「preferred_name」:「Marvin」,「last_name」:「機器人」,「僱主」:「Sirius」,「employstatus」:「Permenant」,「角色」 「位置」: 「空間」, 「辦公電話」: 「」, 「MOBILE_PHONE」: 「」, 「電子郵件」: 「」, 「注意事項」: 「」, 「updated_by」: 「」} – SpeedyH30

相關問題