I am having an issue with posting data to to my view using the django rest framework.
This is what is returned.
// 20150310150130 // http://localhost:8000/orders/order_status_update/
{ "client": [ "Invalid hyperlink - No URL match" ], "order_total_price": [ "This field is required." ] }
View:
@api_view(['PUT'])
def order_status_update(request):
if request.method == 'PUT':
data = {'order_status': request.DATA.get('orderform'),'id': request.DATA.get('orderid'),'url': request.DATA.get('orderid'),'client': request.DATA.get('clientid'), 'order_price':request.DATA.get('')}
print data
serializer = OrderSerializer(data=data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data, status=status.HTTP_201_CREATED)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Serializer:
class OrderSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Order
fields = ('id','url','client','order_status','order_total_price','created')
Model:
class Order(models.Model):
STATUS_CHOICES = (
(u'Not Fullfilled', u'Not Fullfilled'),
(u'Tailoring', u'Tailoring'),
(u'Shipped', u'Shipped'),
)
client = models.ForeignKey('clients.Client')
order_status = models.CharField(max_length=30, choices=STATUS_CHOICES)
order_total_price = models.CharField(max_length=30)
created = models.DateTimeField(auto_now_add=True)
def __unicode__(self):
return unicode(self.id)
For 'client' you have to provide a url, like this:
http://localhost:8000/clients/1/
because you are using a HyperlinkedModelSerializer, and also because this field is a foreign key to client.
As for order_price - there is a mistake in your url, I think, it requires order_total_price and you are giving order_price.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With