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, 2019 in Python by Anirudh
• 2,090 points
3,621 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, 2019 by Nymeria
• 3,560 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, 2019 in Python by Mohammad
• 3,230 points
1,228 views
0 votes
1 answer

how do i print only the last two letters in a string using regular expression in python?

$ to match the end of the ...READ MORE

answered Mar 15, 2019 in Python by Waseem
• 4,540 points
950 views
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 21, 2019 in Python by Shabnam
• 930 points
826 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 22, 2019 in Python by Neha
1,573 views
0 votes
1 answer

How to temporarily disable a foreign key constraint in MySQL?

Hello @kartik, To turn off foreign key constraint ...READ MORE

answered Jun 23, 2020 in Python by Niroj
• 82,840 points
2,507 views
0 votes
1 answer

How do I use Django templates without the rest of Django?

Hello @kartik, Let's say you have this important ...READ MORE

answered Jun 23, 2020 in Python by Niroj
• 82,840 points
1,586 views
0 votes
1 answer

How to do math in a Django template?

Hello @kartik, You can use the add filter: {{ object.article.rating_score|add:"-100" }} Thank ...READ MORE

answered Jun 23, 2020 in Python by Niroj
• 82,840 points
6,559 views
+1 vote
1 answer
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP