Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django - How to check all the attributes of a queryset?

I am looking a way to get all the attributes of a variable after we set the value in it using queryset.

For example...refer below code... using user.id or user.first_name i can get the value for that attribute. But if i want to check what all other attributes it has? Is there a way i can get.

If we use user then it will just return which is what we have defined in admin.py.

Code, i am using Django Shell

from django.contrib.auth.models import User
user=User.objects.get(id=1)
user.first_name # Will return some value say testUser
user.id  # will return some value say 1
like image 875
Mayank Tripathi Avatar asked Dec 03 '22 17:12

Mayank Tripathi


2 Answers

I guessing what you are saying is you want to print all attributes of an object instead of QuerySet

To print all attributes of an object you can do the follow:

from django.contrib.auth.models import User
user=User.objects.get(id=1)
print(user.__dict__)

But if you just what to find out what django default user models fields are, you can check this docs: https://docs.djangoproject.com/en/3.0/ref/contrib/auth/

like image 189
MarkL Avatar answered Jan 03 '23 21:01

MarkL


Django returns a tuple of fields associated with a model if you want. Django 3.0:

from django.contrib.auth.models import User
    User._meta.get_fields()
like image 44
Sadraoui Taha Avatar answered Jan 03 '23 21:01

Sadraoui Taha