Search code examples
djangodjango-database

QuerySet is empty when try to get User from multiple databases


I have a project with multiple databases. I'm trying to fetch all users from one of databases like this:

users = User.objects.using('mydb').all()

or this:

users = User.objects.db_manager('mydb').all()

but get an empty query list instead. <QuerySet [<User: >]>

I've test this with some other models but they are working grate. Also when I've get the count of my records, return the correct number of records.

Am I doing wrong?


Solution

  • but get an empty query list instead. <QuerySet [<User: >]>

    The QuerySet is not empty. Indeed: it has a single User, notice the <User: > part. That user only seems to have an empty username, the <User: > is the repr(..) of that single user. For example:

    >>> User(username='')
    <User: >
    

    You can iterate over it, for example:

    for user in User.objects.using('mydb').all():
        print('a user')

    and it will print 'a user' once.