0

Java クライアントを使用して新しいリソースを django Tastypie API に投稿しようとしていますが、Http 500 エラー コードが表示されます。基本的に、クライアントから API で新しい予約をしようとしています。

モデル:

class Author(models.Model):
    name = models.CharField(max_length=50)
    email = models.EmailField()

    def __unicode__(self):
        return self.name

class Product(models.Model):
    author = models.ForeignKey(Author)

    name = models.CharField(max_length=100)
    genre = models.CharField(max_length=100)
    pub_date = models.DateTimeField()

class Reservation(models.Model):
    user = models.ForeignKey(User)
    product = models.ForeignKey(Product)

    reserv_date_start = models.DateTimeField()
    reserv_finish = models.DateTimeField()
    penalty = models.BooleanField()

    def __unicode__(self):
        return self.product.name

資力:

class AuthorResource(ModelResource):
    #user = fields.ForeignKey(UserResource, 'user')
    class Meta:
        queryset = Author.objects.all()
        resource_name = 'author'
        authentication = BasicAuthentication()
        authorization = DjangoAuthorization()

class ProductResource(ModelResource):
    author = fields.ForeignKey(AuthorResource, 'author')
    class Meta:
        queryset = Product.objects.all()
        resource_name = 'product'
        authentication = BasicAuthentication()
        authorization = DjangoAuthorization()

class ReservationResource(ModelResource):
    product = fields.ForeignKey(ProductResource, 'product')
    class Meta:
        queryset = Reservation.objects.all()
        resource_name = 'reservation'
        authentication = BasicAuthentication()
        authorization = DjangoAuthorization()

私が投稿している私のjson(私はjava simple jsonを使用していて、バックスラッシュを取得していて、それを取り出す方法がわかりません):

public JSONObject encodeJsonObject(Reservation reservation){
    JSONObject obj=new JSONObject();
    obj.put("id",String.valueOf(reservation.getId()));
    obj.put("product","/api/reservation/product/"+reservation.getProduct().getId()+"/");      
    obj.put("reserv_date_start",new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'+00:00'").format(reservation.getReserv_date_start()));
    obj.put("reserv_finish",new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'+00:00'").format(reservation.getReserv_finish()));        
    obj.put("resource_uri", "/api/reservation/reservation/"+reservation.getId()+"/");
    obj.put("penalty",reservation.isPenalty());
    return obj;
}

json: {
"product": "\/api\/reservation\/product\/12\/",
"id": "7",
"reserv_finish": "2013-01-05T23:11:51+00:00",
"resource_uri": "\/api\/reservation\/reservation\/7\/",
"penalty": false,
"reserv_date_start": "2013-01-05T23:11:51+00:00"

}


コードを投稿する私のクライアント:

public void apacheHttpClientPost(String url, String user, char[] pass, JSONObject data) {
     try {

    DefaultHttpClient httpClient = new DefaultHttpClient();
            httpClient.getCredentialsProvider().setCredentials(new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, AuthScope.ANY_REALM),
            new UsernamePasswordCredentials(user, new String(pass)));
    HttpPost postRequest = new HttpPost(url);
    StringEntity input = new StringEntity(data.toJSONString());
    input.setContentType("application/json");
    postRequest.setEntity(input);

    HttpResponse response = httpClient.execute(postRequest);

    if (response.getStatusLine().getStatusCode() != 201) {
        throw new RuntimeException("Failed : HTTP error code : "
            + response.getStatusLine().getStatusCode());
    }

    BufferedReader br = new BufferedReader(
                    new InputStreamReader((response.getEntity().getContent())));

    String output;
    System.out.println("Output from Server .... \n");
    while ((output = br.readLine()) != null) {
        System.out.println(output);
    }

    httpClient.getConnectionManager().shutdown();

  } catch (MalformedURLException e) {

    e.printStackTrace();

  } catch (IOException e) {

    e.printStackTrace();

  }

}

デバッグ エラーの 1 つ:

/usr/local/lib/python2.7/dist-packages/django/db/models/fields/ init .py:808: RuntimeWarning: DateTimeField がナイーブな日時 (2013-01-04 17:31:57) を受け取りましたゾーン サポートがアクティブです。ランタイム警告)

私はに投稿しています

「http://localhost:8000/api/reservation/reservation/」

4

2 に答える 2

1

JSON の日時にタイムゾーン部分がありません:

json: {
    "product": "\/api\/reservation\/product\/9\/",
    "id": "6",
    "reserv_finish": "2013-01-04T17:31:57",                 // <-- Here
    "resource_uri": "\/api\/reservation\/reservation\/6\/",
    "penalty": false,
    "reserv_date_start": "2013-01-04T17:31:57"              // <-- And here
}

ISO-8601 日時は次のようになります。

"2013-01-04T17:31:57+00:00"
                    ^^^^^^^

また、どのpython-dateutilバージョンをインストールしましたか? これを確認していただけますか?

pip freeze | grep dateutil

その他の注目すべき点:

于 2013-01-05T20:43:30.233 に答える
0

とった。問題は不完全な私のjsonにありました。追加していないユーザー リソースへの外部キーがありました。ここに解決策があります:

json:

{
    "product": "\/api\/reservation\/product\/12\/",
    "id": "7",
    "reserv_finish": "2013-01-06T15:26:15+00:00",
    "resource_uri": "\/api\/reservation\/reservation\/7\/",
    "penalty": false,
    "reserv_date_start": "2013-01-06T15:26:15+00:00",
    "user": "\/api\/reservation\/auth\/user\/1\/"
}

資源:

class ReservationResource(ModelResource):
    user = fields.ForeignKey(UserResource, 'user')
    product = fields.ForeignKey(ProductResource, 'product')
    class Meta:
        queryset = Reservation.objects.all()
        resource_name = 'reservation'
        authentication = BasicAuthentication()
        authorization = DjangoAuthorization()

Java クライアント コード:

public JSONObject encodeJsonObject(Reservation reservation){
    JSONObject obj=new JSONObject();
    obj.put("id",String.valueOf(reservation.getId()));  
    obj.put("product","/api/reservation/product/"+reservation.getProduct().getId()+"/");      
    obj.put("reserv_date_start",new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'+00:00'").format(reservation.getReserv_date_start()));
    obj.put("reserv_finish",new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'+00:00'").format(reservation.getReserv_finish()));        
    obj.put("resource_uri", "/api/reservation/reservation/"+reservation.getId()+"/");
    obj.put("user", "/api/reservation/auth/user/1/"); //not dynamic yet
    obj.put("penalty",reservation.isPenalty());
    return obj;
}
于 2013-01-06T15:30:48.967 に答える