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:


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) {
                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>');
                         $('#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,050 points

1 answer to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
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:

   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,500 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
• 1,400 points
0 votes
1 answer
+2 votes
2 answers

In a list of dictionaries, how can I find the minimum calue in a common dictionary field.

There are several options. Here is a ...READ MORE

answered Apr 10, 2018 in Python by charlie_brown
• 7,710 points
0 votes
1 answer

In Python, how do I read a file line-by-line into a list?

with open(fname) as f:     content = f.readlines() # you ...READ MORE

answered Oct 9, 2018 in Python by SDeb
• 10,780 points
0 votes
3 answers
0 votes
1 answer

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.