[英]Django, Role matching query does not exist
As the Title says, The models for Role and Location both have data in them as it was inserted in the admin page and confirmed. 如标题所述,角色和位置的模型都已插入到管理页面中并经过确认,其中都包含数据。 I get the same issue with Location instead of role if it is commented out. 如果被注释掉,我会遇到与位置而不是角色相同的问题。
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
here is the api 这是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')
here is the Model 这是模特
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)
Ad this is the JS stuff 广告这是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"
And here is some more then. 还有更多。 This is the HTML, the part relevant to the location and role 这是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>
this is the drop down js stuff (note Employer works fine) 这是下拉式js的东西(注意Employer可以正常工作)
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>')
And this is the api stuff. 这就是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')
I think one of two things is happening. 我认为正在发生两件事之一。 Possibility one: the values you're posting for Role
and Location
don't match the format you expect. 可能性一:您发布的“ Role
和Location
”值与您期望的格式不匹配。 For instance, the label in the dropdown may be the name
or role_name
but the posted value may be the ID. 例如,下拉菜单中的标签可以是name
或role_name
但发布的值可以是ID。 You mentioned that those data points are selected with a drop-down - double-check the value
attributes in your option
s for that select
to make sure they're really names and not IDs. 你提到的这些数据点与一个下拉列表中选择-仔细检查value
在属性option
■对于select
,以确保他们真的名称,而不是标识。
Or, your jquery function is setting data['role_name']
and data['location']
before a selection is made (or is failing to change the value if the selection is changed). 或者,您的jquery函数在做出选择之前设置了data['role_name']
和data['location']
(或者如果更改了选择,则无法更改该值)。
If you post the value of data
we can figure out which one is the case, but I'm almost certain it's one of these two issues. 如果发布data
的价值,我们可以弄清楚是哪种情况,但我几乎可以肯定这是这两个问题之一。
It is ok world I fixed the issue. 好的,我已经解决了这个问题。 Because I had spaces in my roles and location the dropdowns were the issue so I changed them a bit 因为我在角色和位置上都留有空格,所以下拉菜单是个问题,所以我做了一些更改
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))
I have left the old code in so you can see the fix 我保留了旧代码,以便您可以看到此修复程序
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.