[Answer]-Create Resource on Django Tastypie with a Java POST request but getting 500 error code

0👍

got it. the problem was in my json that was incomplete. i had a foreign key to my user resource that i didn’t add. here is the solution:

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\/"
}

resource:

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 client code:

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;
}

1👍

Datetimes in your JSON are missing the timezone part:

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 datetimes should look like this:

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

Also, which python-dateutil version you have installed? Can you check this please?

pip freeze | grep dateutil

Other things worth looking at:

Leave a comment