How do I access a user by his/her ID using django.contrib.auth.models.User in the latest release of Django?

0 votes

Hi all. I am trying to figure it out  but I am unable to do so and I am stuck.

My requirement is that I want to know how to get the name of any user from the django model - django.contrib.auth.models.User, and doing this only solely on the basis of ID. I also want to delete the user in case I choose to do so, hence I'm trying to use this to do it:

 User.objects.get(id=request.POST['id'])

The thing is it returns a straight-up error and I am presen

User matching query does not exist.

Do note that in this case I am using the ID which is sent by AJAX:

 $("#dynamic-table").on('click','.member_delete_btn', function() {
        if (confirm("Are you sure? the member will be deleted...") == true) {
            $.ajax({
                type: "POST",
                url: "/panel/member/delete/",
                data: { id: $(this).attr('data-id'), 'csrfmiddlewaretoken': '{{ csrf_token }}' },
                success: function (data) {
                     if(data.success) {
                         $('#result').html('<div class="alert alert-success"> <strong>Well done!</strong> Member deleted.</div>');
                         list_members();
                     }else{
                         $('#result').html('<div class="alert alert-warning"> <strong>Warning!</strong> Member not deleted.</div>');
                     }
                },
                error: function (data) {
                    alert("failure:" + data.error);
                }
            });
        }
        else {
            return false;
        }
        return false;
    });

But where I am confused is that when I go ahead and debug it  it seems to be perfectly alright, the user actually exists in the database tables and the other thing is that the ID is perfect and intact (correct)

So my question is simple, how do I do this? Is there any particular delete method meant solely for Django User instances? Or how I go about this on a whole? 

Appreciate all the help, thanks!

Jan 10 in Python by Anirudh
• 2,070 points
42 views

1 answer to this question.

0 votes

Hi, there is only that way and to do it and whatever you are doing is perfectly correct.

But, there is a problem here if you have noticed yet. What if the user you requested does not exist in the schema at all? You have not handled this case at all. I suggest you use the following piece of code for this purpose and it should help with the error, it is a simple exception try/catch block as you can see:

try:
   user_id = int(request.POST['id'])
   user = User.objects.get(id=user_id)
except  User.DoesNotExist:
   //This is to actually handle the faulty case when the user does not exist in the database. 

There is one last step. You need to convert the ID to an integer to get the required output. 

Hope this helps!

answered Jan 10 by Nymeria
• 3,520 points

Related Questions In Python

0 votes
1 answer

how do I check the length of an array in a python program?

lets say we have a list mylist = ...READ MORE

answered Mar 12 in Python by Mohammad
• 3,060 points
44 views
0 votes
1 answer
0 votes
1 answer

How do I run a set of code for all the requests in Flask?

Hi, You can use dedicated hooks(decorators) called before ...READ MORE

answered Jun 20 in Python by Shabnam
• 920 points
23 views
0 votes
1 answer

How do I create a star using the turtle module of python?

Hey @Jinu, Try something like this: import turtle star = ...READ MORE

answered Jun 21 in Python by Neha
46 views
0 votes
3 answers

Python Selenium best tutorials for beginners

Hope this will help you...Python Tutorial READ MORE

answered Feb 11 in Python by aldrinjohn
• 140 points
1,209 views
0 votes
1 answer