2013-07-02 33 views
0

我已經爲我的服務生成了正確的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 
+0

問一個問題,但目前還不清楚,你在找什麼。 – simonmorley

+0

對不起,如果我不清楚。我已更新了一個問題。 – jwhines

+0

你的「RestKit的JSON」,你是如何得到它的?它是實際擊中電線還是源對象的JSON?顯示映射和響應描述符。 – Wain

回答

1

由於您從Charles中粘貼的數據沒有特定格式,因此您是否確實發送了JSON並不清楚。檢查標題和「JSON文本」選項卡(如果存在)。

此外,更改請求描述:

RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:[driverMapping inverseMapping] objectClass:[Driver class] rootKeyPath:nil]; 
+0

json選項卡不存在。我能夠獲得原始數據。這是上面的帖子。 – jwhines

+0

所以你沒有發送JSON。你是否在你的對象管理器上設置了'requestSerializationMIMEType = RKMIMETypeJSON;'? – Wain

+0

它工作。非常感謝。你救了我的命。一旦我添加requestSerializationMIMEType = RKMIMTypeJSON; json被生產出來。 – jwhines

相關問題