我已經爲我的服務生成了正確的json輸出。我已經複製並粘貼restservice瀏覽器中的restkit輸出,它工作得很好。不過,我得到Django JSON錯誤 - 「此字段是必需的」
{"questions": ["This field is required."]}
爲從RKResponsDescriptor響應我的嵌套陣列場試圖從restkit發佈的時候。如果我拿走問題關係,我的帖子可以正常工作。我無法理解爲什麼如果我的json格式對所有正確的字段都是正確的,這篇文章將無法正常工作。我正在爲其餘服務使用django-rest-framework。
如何從Restkit成功發佈問題字段?
這是我的Restkit的json。
{
"questions" : [
{
"question_text" : "What is the question text?",
"question_description" : "What is the question?",
"driver_id" : 9
}
],
"id" : 9,
"first_name" : "Mark",
"phone" : "6783333333",
"last_name" : "Cuban",
"city" : "Decatur",
"email" : "[email protected]",
"home_address" : "3144 Topawa Pl.",
"state" : "Florida",
"zipcode" : "30033"
}
這裏是我的映射:
RKEntityMapping *driverMapping = [RKEntityMapping mappingForEntityForName:@"Driver" inManagedObjectStore:managedObjectStore];
[driverMapping addAttributeMappingsFromDictionary:@{ @"id":@"driverID", @"email":@"email", @"last_name":@"lastName", @"first_name":@"firstName", @"phone":@"phone", @"home_address":@"homeAddress", @"city":@"city", @"state":@"state", @"zipcode":@"zip"}];
driverMapping.identificationAttributes = @[@"driverID"];
RKEntityMapping *questionMapping = [RKEntityMapping mappingForEntityForName:@"Question" inManagedObjectStore:managedObjectStore];
[questionMapping addAttributeMappingsFromDictionary:@{@"driver_id":@"driver.driverID",@"question_description":@"questionDescription", @"question_text":@"questionText"}];
[driverMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"questions" toKeyPath:@"questions" withMapping:questionMapping]];
NSIndexSet *statusCodes = RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful);
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:driverMapping
pathPattern:@"/drivers/"
keyPath:nil
statusCodes:statusCodes];
RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:driverMapping objectClass:[Driver class] rootKeyPath:nil];
[manager addRequestDescriptor:requestDescriptor];
[manager addResponseDescriptor:responseDescriptor];
這裏是我的請求發送到服務器。我用查爾斯來檢索這個。
city Decatur
email [email protected]
first_name Mark
home_address 3144 Topawa Pl.
id 9
last_name Cuban
phone 6783333333
questions[][driver_id] 9
questions[][question_description] What is the question?
questions[][question_text] What is the question text?
state Florida
zipcode 30033
以下是來自Charles的原始數據。
POST /drivers/ HTTP/1.1
Host: localhost:8888
Authorization: Basic andoaW5lczpXZWJzdGVycGllMQ==
Content-Type: application/x-www-form-urlencoded; charset=utf-8
Accept-Encoding: gzip, deflate
Accept-Language: en;q=1, fr;q=0.9, de;q=0.8, ja;q=0.7, nl;q=0.6, it;q=0.5
Accept: application/json
Content-Length: 304
Connection: keep-alive
User-Agent: mydriver/1.0 (iPad Simulator; iOS 6.1; Scale/2.00)
city=Decatur&email=cuban%40gmail.com&first_name=Mark&home_address=3144%20Topawa%20Pl.&id=9&last_name=Cuban&phone=6783333333&questions[][driver_id]=9&questions[][question_description]=What%20is%20the%20question%3F&questions[][question_text]=What%20is%20the%20question%20text%3F&state=Florida&zipcode=30033
問一個問題,但目前還不清楚,你在找什麼。 – simonmorley
對不起,如果我不清楚。我已更新了一個問題。 – jwhines
你的「RestKit的JSON」,你是如何得到它的?它是實際擊中電線還是源對象的JSON?顯示映射和響應描述符。 – Wain