django migrations“LookupError:没有安装应用程序......”用于初始数据迁移

时间:2016-05-27 08:01:58

标签: python django django-migrations

我想使用迁移为用户个人资料提供初始数据。但不知何故,我用来扩展用户模型的应用程序在查找时失败了。 UserProfile类模型与auth.User& amp;与其他应用程序的类模型相关的一对多。有人有任何想法解决我的问题吗?

  • python版本:2.7
  • django版本:1.8.11

这是进行测试时的追溯:

File "./manage.py", line 10, in <module>
  execute_from_command_line(sys.argv)
File "/home/pupil/.virtualenvs/ibes18/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 354, in execute_from_command_line
  utility.execute()
File "/home/pupil/.virtualenvs/ibes18/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 346, in execute
  self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/pupil/.virtualenvs/ibes18/local/lib/python2.7/site-packages/django/core/management/base.py", line 394, in run_from_argv
  self.execute(*args, **cmd_options)
File "/home/pupil/.virtualenvs/ibes18/local/lib/python2.7/site-packages/django/core/management/base.py", line 445, in execute
  output = self.handle(*args, **options)
File "/home/pupil/.virtualenvs/ibes18/local/lib/python2.7/site-packages/django/core/management/commands/migrate.py", line 222, in handle
  executor.migrate(targets, plan, fake=fake, fake_initial=fake_initial)
File "/home/pupil/.virtualenvs/ibes18/local/lib/python2.7/site-packages/django/db/migrations/executor.py", line 110, in migrate
  self.apply_migration(states[migration], migration, fake=fake, fake_initial=fake_initial)
File "/home/pupil/.virtualenvs/ibes18/local/lib/python2.7/site-packages/django/db/migrations/executor.py", line 148, in apply_migration
  state = migration.apply(state, schema_editor)
File "/home/pupil/.virtualenvs/ibes18/local/lib/python2.7/site-packages/django/db/migrations/migration.py", line 115, in apply
  operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
File "/home/pupil/.virtualenvs/ibes18/local/lib/python2.7/site-packages/django/db/migrations/operations/special.py", line 183, in database_forwards
  self.code(from_state.apps, schema_editor)
File "/home/pupil/Coding/estimator/IBES/ibes_initial_data/migrations/0001_ibes_initial_data_user_profile.py", line 6, in set_home_branch_user
  UserProfile = apps.get_model("users.UserProfile")
File "/home/pupil/.virtualenvs/ibes18/local/lib/python2.7/site-packages/django/apps/registry.py", line 202, in get_model
  return self.get_app_config(app_label).get_model(model_name.lower())
File "/home/pupil/.virtualenvs/ibes18/local/lib/python2.7/site-packages/django/apps/registry.py", line 150, in get_app_config
  raise LookupError("No installed app with label '%s'." % app_label)
LookupError: No installed app with label 'users'.

以下是我的一些代码:

# 0001_initial_user_profile.py
from django.db import migrations


def set_profile_user(apps, schema_editor):
    Branch = apps.get_model("multiple", "Branch")
    UserProfile = apps.get_model("users", "UserProfile")
    User = apps.get_registered_model("auth", "User")
    user = User.objects.get(username="test_user")
    branch = Branch.objects.get(id=1)
    db_alias = schema_editor.connection.alias
    UserProfile.objects.using(db_alias).create(
        user=user, home_branch=branch
    )


class Migration(migrations.Migration):
    dependencies = [
        ("multiple", "0001_initial"),
        ("auth", "0006_require_contenttypes_0002"),
        ("ibes_initial_data", "0001_ibes_initial_data_server")
    ]
    operations = [migrations.RunPython(set_home_branch_user)]


# users/models.py

...
class UserProfile(SynchronizableModel):
    from jobs.models.models_master import Merchant
    user = models.OneToOneField(User, unique=True)
    home_branch = models.ForeignKey(Branch)
    max_jobs = models.PositiveSmallIntegerField(default=1)
    merchant_access = models.ManyToManyField(Merchant)
...

# multiple/models.py

...
class Branch(models.Model):
    name = models.CharField(max_length=100, null=False, blank=False)
    created_by = models.ForeignKey(User)
    modified_by = models.ForeignKey(User)

    def __unicode__(self):
        return self.name
...

# settings.py
INSTALLED_APPS = (
...
    'users',
...
)

0 个答案:

没有答案