Mox February 2016

django rest framework invalid literal for int() with base 10:

The following RESTful API failed with an error that I have no idea what is going on there.

Here is the construct of the serializer, model and the url pattern.

serializers.py

class PatientDataSerializer(serializers.ModelSerializer):
    class Meta:
        model = PatientData
        fields = (
            'data_id',
            'data_type',
            'session_id',
            'user_id',
            'start_time',
            'time_elapsed',
            'data_file_url'
        )
        lookup_field='user_id'

models.py

class PatientData(models.Model):
    class Meta:
        managed = False
        db_table = "patient_data_list"

    data_id = models.PositiveIntegerField(db_column="data_id", primary_key=True)
    data_type = models.CharField(db_column="patient_data_type", max_length=255)
    session_id = models.PositiveIntegerField(db_column="session_id")
    user_id = models.PositiveIntegerField(db_column="user_id")
    start_time = models.DateTimeField(db_column="start_time")
    time_elapsed = models.IntegerField(db_column="time_elapsed")
    data_file_url = models.IntegerField(db_column="data_file_url")  

urls.py

 url(r'^patients/data/(?P<user_id>[0-9]+)/$', restapiviews.PatientDataListView.as_view()),

restapiviews.py

class PatientDataListView(generics.ListAPIView):
    queryset = PatientData.objects.all()
    serializer_class = PatientDataSerializer
    lookup_field='user_id'

The error appears when i tried to access the localhost:8080/api/patient/data/1/ url

Answers


kacperd February 2016

This error means that your are trying to cast empty string or unicode to int.

I was able to run your code locally and I suspect that this happens because in your database there are empty value in fields that should be Integers. DRF IntegerField's to_representation method requires makes a simple casting to int, and doesn't check for empty values. Please check your data in this manner, and if this is not the point paste your whole traceback with an error.

Post Status

Asked in February 2016
Viewed 3,239 times
Voted 5
Answered 1 times

Search




Leave an answer