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

4

10

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
Behave answered 28/4, 2020 at 4:29 Comment(0)
E
6

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()
Experientialism answered 28/4, 2020 at 4:40 Comment(1)
._meta.get_fields(), this will return all the fields along with the relationship with other models as well.. cool. This is very helpful to check the relationship of the model. Thanks a lot.Behave
T
14

I’m guessing what you are saying is that 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 want 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/

Trichinosis answered 28/4, 2020 at 4:34 Comment(3)
Perfect.. this is what I was looking for. Thanks a lot.Behave
This .__dict__ can be used to any variable (which holds the data) not just the user as above.Behave
@MayankTripathi you should probably mark this as the answer that solved your problem then.Chrysa
E
6

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()
Experientialism answered 28/4, 2020 at 4:40 Comment(1)
._meta.get_fields(), this will return all the fields along with the relationship with other models as well.. cool. This is very helpful to check the relationship of the model. Thanks a lot.Behave
G
6

Adding to @MarkL 's answer: You can pretty print it for better readability.

from django.contrib.auth.models import User
from pprint import pprint # new
user=User.objects.get(id=1)
pprint(user.__dict__) # new ---->  pprint() instead of print()

Another way which I always prefer using over __dict__ is vars() method:

user=User.objects.get(id=1)
pprint(vars(user)) 

Both return the same result but to me vars() is more convenient to write than the dunder dict method i-e __dict__, coz I am too lazy to write 4 underscores.

Gobo answered 9/3, 2021 at 12:57 Comment(0)
L
1

Building on MarkL's answer, here is the same thing, but with nicer formatting:

[f"{key}: {value}" for key, value in user.__dict__.items()]

(Sorry, I don't have enough rep to post this as a comment.)

Lhary answered 18/9, 2020 at 20:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.