Reputation: 198
I am doing a small project in Django with the rest framework and I reached a point where I don't really know how to continue.
In the application, my customers can order food or drinks, which are then added to the order. I have the following models:
Orders
order_id = models.AutoField(primary_key = True)
customer = models.ForeignKey(Customers, on_delete = None)
date = models.DateTimeField(auto_now_add=True)
The model Customers is just a OneToOneField from the User model.
Food Orders
fake_id = models.AutoField(primary_key = True)
order_id = models.ForeignKey(Orders, on_delete = models.CASCADE)
food_id = models.ForeignKey(Food, on_delete = models.CASCADE)
quantity = models.IntegerField()
Drink Orders
fake_id = models.AutoField(primary_key = True)
order_id = models.ForeignKey(Orders, on_delete = models.CASCADE)
drink_id = models.ForeignKey(Drink, on_delete = models.CASCADE)
quantity = models.IntegerField()
So with my models in mind, first I need to create an order, and then create a food order or drink order for each item in the order. The problem I am having is I can't even create the order. I created a serializer and a view, pass the data with postman and nothing, I always get an error.
My serializer:
class OrdersSerializer(serializers.Serializer):
customer = UserSerializer()
date = serializers.DateTimeField()
class Meta:
model = Orders
def create(self, validated_data):
order = Orders.objects.create(**validated_data)
order.save()
return order
My view:
@api_view(['POST'])
def createOrder(request):
ser = OrdersSerializer(data=request.data)
if ser.is_valid():
ser.save()
return Response(ser.data, status=status.HTTP_201_CREATED)
else:
return Response(ser._errors, status=status.HTTP_400_BAD_REQUEST)
This is the payload I am trying to send:
{
"date": [{
"date": "2013-01-29T12:34:56.000000Z"
}],
"customer": {
"id": [{
"id":1
}]
}
}
And this is the response I get:
{
"customerID": {
"id": [
"A valid integer is required."
]
},
"date": [
"Datetime has wrong format. Use one of these formats instead: YYYY-MM-DDThh:mm[:ss[.uuuuuu]][+HH:MM|-HH:MM|Z]."
]
}
I feel it may be a problem with the payload, but I have really been trying to get it to work with no avail.
Upvotes: 3
Views: 324
Reputation: 635
The payload format is incorrect. This should be the correct format:
{
"customer": { "id": 1 },
"date": "2018-09-04 06:00:00.000000+05:30"
}
Also, you need to make sure that a customer with id=1
exists in the DB.
Upvotes: 0
Reputation: 2288
Seems like your payload has an extra layer of nesting... It should be
{
"date": "2013-01-29T12:34:56.000000Z",
"customer": {
"id":1
}
}
This should be similar to the output of ser.data
On a side note, in case those are your actual models you might want to rename the ForeignKey fields to not have the _id
suffix...
Django will automatically create a hidden field with _id
(in your case order_id_id
) which is the db field, and the field name without the _id
suffix would be an actual model object. So in your case order_id
would be an Order object, and not the id. This can be confusing.
Upvotes: 2