django dumpdata empty array
Asked Answered
B

5

8

I am fairly new to django and I am using it to make a website for an online game. The game already has it's own auth stuff so I am using that as a custom auth model for django.

I created a new app called 'account' to put this stuff in and added the models. I added the router and enabled it in the settings and everything works good, I can log in from the admin site and do stuff.

Now I am also trying to learn TDD, so I need to dump the auth database to a fixture. When I run ./manage.py dumpdata account i get an empty array back. There aren't any errors or any trace back what so ever, just an empty array. I've fiddled with it the best I can but I can't seem to find what the issue is.

Here are some relevant settings.

Database

DATABASES = {  
    'default': {  
        'ENGINE': 'django.db.backends.postgresql_psycopg2',  
        'NAME': 'censored',  
        'USER': 'censored',  
        'PASSWORD': 'censored',  
        'HOST': 'localhost',  
        'PORT': '',  
    },    
    'auth_db': {  
        'ENGINE': 'mysql_pymysql',  
        'NAME': 'censored',  
        'USER': 'censored',  
        'PASSWORD': 'censored',  
        'HOST': '127.0.0.1',  
        'PORT': '3306'  
    }  
}

Router

class AccountRouter(object):
    """
    A router to control all database operations on models in the
    account application.
    """
    def db_for_read(self, model, **hints):
        """
        Attempts to read account models go to auth_db.
        """
        if model._meta.app_label == 'account':
            return 'auth_db'
        return None

    def db_for_write(self, model, **hints):
        """
        Attempts to write account models go to auth_db.
        """
        if model._meta.app_label == 'account':
            return 'auth_db'
        return None

    def allow_relation(self, obj1, obj2, **hints):
        """
        Allow relations if a model in the account app is involved.
        """
        if obj1._meta.app_label == 'account' or \
           obj2._meta.app_label == 'account':
                return True
        return None

    def allow_syncdb(self, db, model):
        """
        Make sure the account app only appears in the 'auth_db'
        database.
        """
        if model._meta.app_label == 'account':
            return False
        return None

Django Settings

DATABASE_ROUTERS = ['account.router.AccountRouter']

I am really at a loss for what to try, any help or ideas are appreciated.

Blenheim answered 13/8, 2013 at 19:19 Comment(0)
S
6

I also had the same issue, you need to specify the correct database. For example, given your code:

$ ./manage.py dumpdata --database=auth_db account
Sulky answered 5/11, 2013 at 13:4 Comment(0)
B
6

I had similar problem. Creating an empty file called models.py solved the problem for me. Check if you have such a file in your app catalog and if not - create one.

Bove answered 3/10, 2017 at 8:56 Comment(1)
I don't understand this solution. How can there be data in the app in the first place, if there isn't already a models.py file? This is the second time I find this proposed answer, but it seems like there is something missing in it. Where should this new models.py file reside?Tabitha
G
2
  • Make sure the model goes correct. if the model has an error, the ./manage.py dumpdata command will keep silent while running and output []. So the suggestion is to run the models' code in ./manage.py shell and the target data exists, for example:

from account.models import Account print Account.objects.all()[:1]

  • Make sure ./manage.py dumpdata can find the targe model. Django finds models via {APP_NAME}.models, if you place your models in directory account/models/, import your models in account/models/__init__.py, for example:from profile import Profile
Genna answered 22/4, 2015 at 10:33 Comment(0)
H
1

I had a similar issue and this old answer in another Stackoverflow thread helped me more than the answers above: the issue was indeed related to a misconfiguration of my router, more specifically to allow_migrate which should return True for the given model, the given app and the given database.

I believe this is due to this line (Django source code on Github) of the command dumpdata.

Hester answered 12/6, 2020 at 10:13 Comment(1)
Finally, after hours, I've noticed your hint to allow_migrate. Maybe my upvote can help others notice this earlier...Miticide
U
0

If you have an app structure where models.py is not in the app root dir, just add a blank one in the app root dir

Unsupportable answered 8/9, 2022 at 4:55 Comment(2)
How is this different from the already existing answer by Tomasz?Disfranchise
It clarifies that the models.py could be in the app but in a different place other than the app root dir. I would have made it as a comment in response to Teekin's question under Tomasz's answer but I don't have enough reputation to do thatUnsupportable

© 2022 - 2024 — McMap. All rights reserved.