[Answered ]-Django queryset select all from multiple tables

1👍

If you have defined the ForeignKey relationships on the User class, you can use:

users = User.objects.select_related("Address", "Contact").all()

Then you can access the related object on the User object as as attribute:

for user in users:
    print(user.address)
    print(user.contact)

Actually, the select_related() hint is not necessary and you can access the related record as an attribute in any case, it just prevents a separate database query for every access.

If you are using the User model in the Django auth app, you can go either of these routes to achieve your goal:

I hope this solves your problem.

👤zaadeh

Leave a comment