0

I've been trying to delete an existent instance from the user table (mysql) but I keep getting error from the user.delete() this is the my viewpoint:

@api_view(['GET', 'PUT', 'DELETE'])
def user_api_id(request):
    try:
        id = request.GET.get('id')
        user = User.objects.get(pk=id)
        if request.method == 'GET':
            serializer = UserSerializer(user)
            return Response(serializer.data)

        elif request.method == 'PUT':
            serializer = UserSerializer(user, data=request.data)
            if serializer.is_valid():
                serializer.save()
                return Response(serializer.data)
            return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

        elif request.method == 'DELETE':
            try:
                user.delete()
                return Response(status=status.HTTP_204_NO_CONTENT)
            except ValidationError as ve:
                return Response({"error": str(ve)}, status=status.HTTP_400_BAD_REQUEST)
            except Exception as e:
                return Response({"error": str(e)}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
    except User.DoesNotExist:
        return Response(status=status.HTTP_404_NOT_FOUND)

When I launch a DELETE request I get this:

"error": "(1292, \"Truncated incorrect DOUBLE value: 'john'\")"

(btw the user I want to delete has id='john')

This is my User model:

class User(models.Model):

    id = models.CharField(primary_key=True, max_length=50)

    password = models.CharField(max_length=50)

    name = models.CharField(max_length=25)

    lastname = models.CharField(max_length=25)

    age = models.IntegerField()

    gender = models.CharField(max_length=6)

    email = models.CharField(max_length=50, blank=True, null=True)

    phone = models.CharField(max_length=50, blank=True, null=True)

    address = models.CharField(max_length=50, blank=True, null=True)

    educationallevel = models.CharField(db_column='educationalLevel', max_length=50, blank=True, null=True)  # Field name made lowercase.

    experience = models.CharField(max_length=20, blank=True, null=True)
    is_active = models.BooleanField(default=True)
    
    is_anonymous = models.BooleanField(default=False)
    is_authenticated = models.BooleanField(default=True)
    
    
    USERNAME_FIELD = 'id'
    REQUIRED_FIELDS = []
    

    class Meta:

        managed = False

        db_table = 'user'

And this is the user table from the db:

CREATE TABLE `user` (
  `id` varchar(50) COLLATE utf8mb4_general_ci NOT NULL,
  `password` varchar(50) COLLATE utf8mb4_general_ci NOT NULL,
  `name` varchar(25) COLLATE utf8mb4_general_ci NOT NULL,
  `lastname` varchar(25) COLLATE utf8mb4_general_ci NOT NULL,
  `age` int NOT NULL,
  `gender` enum('male','female') COLLATE utf8mb4_general_ci NOT NULL,
  `email` varchar(50) COLLATE utf8mb4_general_ci DEFAULT NULL,
  `phone` varchar(50) COLLATE utf8mb4_general_ci DEFAULT NULL,
  `address` varchar(50) COLLATE utf8mb4_general_ci DEFAULT NULL,
  `educationalLevel` varchar(50) COLLATE utf8mb4_general_ci DEFAULT NULL,
  `experience` varchar(20) COLLATE utf8mb4_general_ci DEFAULT NULL,
  `is_anonymous` tinyint(1) DEFAULT '0',
  `is_authenticated` tinyint(1) DEFAULT '1',
  `is_active` tinyint(1) DEFAULT '0',
  PRIMARY KEY (`id`)
)

I tried deleting directly from the database and it works as expected. Tried passing a non-existent id too and it returned User DOESNOTEXIT as expected. Also, the id field from user table is referenced as a foreign key in other tables in db but only the user table has data.

usef
  • 25
  • 4
  • problem is that you are trying request.GET.get in a DELETE and POST request too, can you also add the endpoint you are calling for this? for better understanding – SimbaOG Aug 04 '23 at 08:52
  • Im sending the request via postman DELETE http://localhost:8000/user_api_id/?id=john the url path in urls.py is : `path('user_api_id/', user_api_id, name='user_api_id'),` but I don't think that's the issue because when I try to print out the user (john) just before the exception happens it displays the user no problem, meaning the issue is not in finding the user, it's in deleting it I believe. – usef Aug 04 '23 at 14:21

1 Answers1

0

I somehow solved the problem by turning 'django.contrib.admin' in the INSTALLED_APPS into a comment.

usef
  • 25
  • 4