gpt4 book ai didi

javascript - Django, Angular 色匹配查询不存在

转载 作者:行者123 更新时间:2023-12-03 08:12:59 26 4
gpt4 key购买 nike

正如标题所说, Angular 色和位置的模型都包含在管理页面中插入并确认的数据。如果被注释掉,我会遇到与位置而不是 Angular 色相同的问题。

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,与位置和 Angular 色相关的部分

<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')

最佳答案

我认为有两件事正在发生之一。可能性一:您发布的 RoleLocation 值与您期望的格式不匹配。例如,下拉列表中的标签可能是namerole_name,但发布的值可能是ID。您提到这些数据点是通过下拉菜单选择的 - 仔细检查选项中的value属性以进行选择确保它们确实是名称而不是 ID。

或者,您的 jquery 函数在进行选择之前正在设置 data['role_name']data['location'](或者无法更改值)如果选择更改)。

如果您发布 data 的值,我们可以找出属于哪一种情况,但我几乎可以肯定这是这两个问题之一。

关于javascript - Django, Angular 色匹配查询不存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34071830/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com