0

Edit

I suspect the whole problem with my UpdateApiView is with the url. No matter how I change it, will return 404.

url(r'verify-phone/(?P<phone_id>^\d+)$', view.VerifyPhone.as_view(), name='verify-phone'),

it returns

{
    "detail": "Not found."
}


[18/Apr/2016 01:39:02] "PATCH /api/verify-phone/phone_id=00980 HTTP/1.1" 404 4941

Why?

views.py

class VerifyPhone(generics.UpdateAPIView):
    permission_classes = (AllowAny,)
    serializer_class = serializers.VerifyPhoneSerializer
    allowed_methods = ['PATCH']
    lookup_field = 'phone_id'

    def get_queryset(self):
        phone_id = self.request.query_params.get('phone_id', None)
        queryset = User.objects.filter(phone_id=phone_id)
        return queryset

def update(self, request, *args, **kwargs):
    print('inside update')
    print(request.data)
    partial = kwargs.pop('partial', False)
    instance = self.get_object()
    print(instance)
    serializer = self.get_serializer(instance, data=request.data, partial=partial)
    print(serializer)
    serializer.is_valid(raise_exception=True)
    self.perform_update(serializer)
    print('done perform update')
    return Response(serializer.data)

serializers.py

class VerifyPhoneSerializer(serializers.ModelSerializer):
    regex = r'\d+'
    verification_code = serializers.RegexField(regex, max_length=7, min_length=7, allow_blank=False)
    phone_id = serializers.HyperlinkedRelatedField(view_name='verify-phone', lookup_field='phone_id', read_only=True)

    class Meta:
        model = User
        fields = ('verification_code', 'phone_id')

    def validate(self, data):
        verification = api.tokens.verify(data['phone_id'], data['verification_code'])

        if verification.response.status_code != 200:
            raise serializers.ValidationError("Invalid verification code.")

        return data

def update(self, instance, validated_data):
    instance.phone_number_validated = True
    instance.save()
    return instance

Second question Is this correct to get phone_id from the views?

phone_id = serializers.HyperlinkedRelatedField(view_name='verify-phone', lookup_field='phone_id', read_only=True)
2
  • i just deleted the whole view in this question. Can you go look at the history? The reason I deleted the whole code is because it's unnecessary. But will add if you think so. Commented Apr 18, 2016 at 18:46
  • and why do you down voted this question? Commented Apr 18, 2016 at 18:47

1 Answer 1

1

Looking at your api url def, I think you should call:

/api/verify-phone/00980

instead of

/api/verify-phone/phone_id=00980

I also think something is wrong with the url def itself (the ^ before \d):

url(r'verify-phone/(?P<phone_id>^\d+)$', view.VerifyPhone.as_view(), name='verify-phone')

should be

url(r'verify-phone/(?P<phone_id>\d+)$', view.VerifyPhone.as_view(), name='verify-phone')

or

url(r'verify-phone/(?P<phone_id>\d{5})$', view.VerifyPhone.as_view(), name='verify-phone')
Sign up to request clarification or add additional context in comments.

2 Comments

So? Was it the issue?
it wasn't the issue. The problem was entirely different thing that I wasn't aware of at that time and the question is not related at all. I'll accept anyway for your good effort to help :).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.