Django dumpdata empty array

I am new to django and I use it to create a website for an online game. The game already has its own auth stuff, so I use this as a custom auth model for django.

I created a new application called "account" to insert this material and added models. I added a router and turned it on in the settings, and everything works fine, I can log in from the admin site and do something.

Now I'm also trying to learn TDD, so I need to dump the auth database on the device. When I run ./manage.py dumpdata account , I get an empty array. There are no errors or any traces back that ever, just an empty array. I have worked with this best that I can, but I can not find that problem.

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.

+4
source share
3 answers

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

 $ ./manage.py dumpdata --database=auth_db account 
+3
source
  • Make sure the model is correct. if the model has an error, the ./manage.py dumpdata will be silent during operation and exit [] . Therefore, it is proposed to run the model 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 through {APP_NAME}.models , if you put your models in the account/models/ directory, import your models in account/models/__init__.py , for example: from profile import Profile
+2
source

I had a similar problem. Creating an empty file called models.py solved the problem for me. Check if you have such a file in the application directory, and if not, create it.

0
source

Source: https://habr.com/ru/post/1496892/


All Articles