Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django Rest Framework Hyperlink Error

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)
like image 863
Dave Avatar asked Nov 10 '22 17:11

Dave


1 Answers

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.

like image 179
Karina Klinkevičiūtė Avatar answered Nov 14 '22 22:11

Karina Klinkevičiūtė