自定义用户模型ValueError:相关模型Django(1.9)

时间:2016-08-07 20:43:17

标签: python django django-models

当我尝试将CustomUser模型作为ForiegnKey添加到django中的字段时出错。身份验证使用CustomUser模型,但出于某种原因我收到此错误:

ValueError: Related model 'authentication.UserModel' cannot be resolved

这是app / models.py:

 from django.db import models
 from django.conf import settings



 class A(models.Model)
    tender_authority = models.ForeignKey(settings.AUTH_USER_MODEL)

这是settings.py

AUTH_USER_MODEL = 'authentication.UserModel'

这是项目的树结构:

myproject
   settings.py
authentication
   models.py ---> UserModel (my custom user model name) Model is present here
myapp
   models.py ---> Error happening here

提前谢谢你。

完全跟踪,忘记在我尝试迁移时发生这种情况:

Operations to perform:
  Apply all migrations: auth, admin, authentication, contenttypes,         sessions, Forms
Running migrations:
   Rendering model states... DONE
   Applying Forms.0010_tech_auth_Usermodel_to_User...Traceback (most recent call last):
 File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
 File "/usr/local/lib/python3.5/dist-packages/django/core/management/__init__.py", line 353, in execute_from_command_line
utility.execute()
  File "/usr/local/lib/python3.5/dist-packages/django/core/management/__init__.py", line 345, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/local/lib/python3.5/dist-packages/django/core/management/base.py", line 348, in run_from_argv
self.execute(*args, **cmd_options)
  File "/usr/local/lib/python3.5/dist-packages/django/core/management/base.py", line 399, in execute
output = self.handle(*args, **options)
  File "/usr/local/lib/python3.5/dist-packages/django/core/management/commands/migrate.py", line 200, in handle
executor.migrate(targets, plan, fake=fake, fake_initial=fake_initial)
  File "/usr/local/lib/python3.5/dist-packages/django/db/migrations/executor.py", line 92, in migrate
self._migrate_all_forwards(plan, full_plan, fake=fake, fake_initial=fake_initial)
  File "/usr/local/lib/python3.5/dist-packages/django/db/migrations/executor.py", line 121, in _migrate_all_forwards
state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
  File "/usr/local/lib/python3.5/dist-packages/django/db/migrations/executor.py", line 198, in apply_migration
state = migration.apply(state, schema_editor)
  File "/usr/local/lib/python3.5/dist-packages/django/db/migrations/migration.py", line 123, in apply
operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
  File "/usr/local/lib/python3.5/dist-packages/django/db/migrations/operations/fields.py", line 201, in database_forwards
schema_editor.alter_field(from_model, from_field, to_field)
  File "/usr/local/lib/python3.5/dist-packages/django/db/backends/base/schema.py", line 454, in alter_field
new_db_params = new_field.db_parameters(connection=self.connection)
  File "/usr/local/lib/python3.5/dist-packages/django/db/models/fields/related.py", line 967, in db_parameters
return {"type": self.db_type(connection), "check": []}
  File "/usr/local/lib/python3.5/dist-packages/django/db/models/fields/related.py", line 958, in db_type
rel_field = self.target_field
  File "/usr/local/lib/python3.5/dist-packages/django/db/models/fields/related.py", line 861, in target_field
return self.foreign_related_fields[0]
  File "/usr/local/lib/python3.5/dist-packages/django/db/models/fields/related.py", line 594, in foreign_related_fields
return tuple(rhs_field for lhs_field, rhs_field in self.related_fields if rhs_field)
  File "/usr/local/lib/python3.5/dist-packages/django/db/models/fields/related.py", line 581, in related_fields
self._related_fields = self.resolve_related_fields()
  File "/usr/local/lib/python3.5/dist-packages/django/db/models/fields/related.py", line 566, in resolve_related_fields
raise ValueError('Related model %r cannot be resolved' % self.remote_field.model)
ValueError: Related model 'authentication.UserModel' cannot be resolved

迁移:

类迁移(migrations.Migration):

dependencies = [
    ('Forms', '0009_revised_tender_changes'),
]

operations = [
    migrations.AlterField(
        model_name='agreementsanctionmodel',
        name='bank_details_one',
        field=models.ForeignKey(blank=True, on_delete=django.db.models.deletion.CASCADE, to='Forms.BankDetailsModel', verbose_name='Bank Details'),
    ),
    migrations.AlterField(
        model_name='agreementsanctionmodel',
        name='contractor',
        field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, to='Forms.Contractor', verbose_name='Contractor'),
    ),
    migrations.AlterField(
        model_name='agreementsanctionmodel',
        name='contractor_name',
        field=models.CharField(blank=True, max_length=250, verbose_name='Contractor'),
    ),
    migrations.AlterField(
        model_name='agreementsanctionmodel',
        name='created_on',
        field=models.DateTimeField(auto_now=True, verbose_name='Created On'),
    ),
    migrations.AlterField(
        model_name='agreementsanctionmodel',
        name='financial_year',
        field=models.CharField(blank=True, max_length=5, verbose_name='Financial Year'),
    ),
    migrations.AlterField(
        model_name='agreementsanctionmodel',
        name='tender',
        field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, to='Forms.Tender', verbose_name='Tender'),
    ),
    migrations.AlterField(
        model_name='agreementsanctionmodel',
        name='tender_name',
        field=models.CharField(blank=True, max_length=50, unique=True, verbose_name='Tender'),
    ),
    migrations.AlterField(
        model_name='agreementsanctionmodel',
        name='updated_on',
        field=models.DateField(blank=True, default=None, null=True, verbose_name='Updated On'),
    ),
    migrations.AlterField(
        model_name='consultant',
        name='districts',
        field=models.CharField(max_length=50, null=True, verbose_name='District'),
    ),
    migrations.AlterField(
        model_name='consultant',
        name='email',
        field=models.EmailField(blank=True, max_length=254, null=True, unique=True, verbose_name='Email ID'),
    ),
    migrations.AlterField(
        model_name='consultant',
        name='first_name',
        field=models.CharField(default=0, max_length=100, verbose_name='First Name'),
        preserve_default=False,
    ),
    migrations.AlterField(
        model_name='consultant',
        name='house_number',
        field=models.CharField(blank=True, max_length=50, null=True, verbose_name='Phone Number'),
    ),
    migrations.AlterField(
        model_name='consultant',
        name='last_name',
        field=models.CharField(max_length=100, verbose_name='Last Name'),
    ),
    migrations.AlterField(
        model_name='consultant',
        name='middle_name',
        field=models.CharField(blank=True, max_length=100, null=True, verbose_name='Middle Name'),
    ),
    migrations.AlterField(
        model_name='consultant',
        name='pan_number',
        field=models.CharField(default=0, max_length=50, unique=True, verbose_name='PAN Number'),
        preserve_default=False,
    ),
    migrations.AlterField(
        model_name='consultant',
        name='state',
        field=models.CharField(max_length=50, null=True, verbose_name='State'),
    ),
    migrations.AlterField(
        model_name='consultant',
        name='street_name',
        field=models.CharField(max_length=50, null=True, verbose_name='Street Name'),
    ),
    migrations.AlterField(
        model_name='consultant',
        name='tin_number',
        field=models.CharField(default=0, max_length=10, unique=True, verbose_name='TIN Number'),
        preserve_default=False,
    ),
    migrations.AlterField(
        model_name='contractor',
        name='districts',
        field=models.CharField(max_length=50, null=True, verbose_name='District'),
    ),
    migrations.AlterField(
        model_name='contractor',
        name='email',
        field=models.EmailField(blank=True, max_length=254, null=True, unique=True, verbose_name='Email ID'),
    ),
    migrations.AlterField(
        model_name='contractor',
        name='first_name',
        field=models.CharField(max_length=100, verbose_name='First Name'),
    ),
    migrations.AlterField(
        model_name='contractor',
        name='house_number',
        field=models.CharField(blank=True, max_length=50, null=True, verbose_name='Phone Number'),
    ),
    migrations.AlterField(
        model_name='contractor',
        name='last_name',
        field=models.CharField(max_length=100, verbose_name='Last Name'),
    ),
    migrations.AlterField(
        model_name='contractor',
        name='middle_name',
        field=models.CharField(blank=True, max_length=100, null=True, verbose_name='Middle Name'),
    ),
    migrations.AlterField(
        model_name='contractor',
        name='state',
        field=models.CharField(max_length=50, null=True, verbose_name='State'),
    ),
    migrations.AlterField(
        model_name='contractor',
        name='street_name',
        field=models.CharField(max_length=50, null=True, verbose_name='Street Name'),
    ),
    migrations.AlterField(
        model_name='locationmodel',
        name='district',
        field=models.CharField(max_length=50, verbose_name='District '),
    ),
    migrations.AlterField(
        model_name='locationmodel',
        name='division',
        field=models.CharField(max_length=50, verbose_name='Division '),
    ),
    migrations.AlterField(
        model_name='locationmodel',
        name='location',
        field=models.CharField(max_length=50, verbose_name='Location'),
    ),
    migrations.AlterField(
        model_name='locationmodel',
        name='place',
        field=models.CharField(max_length=50, verbose_name='Place '),
    ),
    migrations.AlterField(
        model_name='locationmodel',
        name='work',
        field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, to='Forms.WorkModel', verbose_name='Work'),
    ),
    migrations.AlterField(
        model_name='nominationmodel',
        name='created_on',
        field=models.DateTimeField(auto_now=True, verbose_name='Created On'),
    ),
    migrations.AlterField(
        model_name='nominationmodel',
        name='tender_authority',
        field=models.ForeignKey(blank=True, on_delete=django.db.models.deletion.CASCADE, to='Forms.TechnicalAuthority', verbose_name='Technical Authority'),
    ),
    migrations.AlterField(
        model_name='nominationmodel',
        name='updated_on',
        field=models.DateTimeField(blank=True, null=True, verbose_name='Updated On'),
    ),
    migrations.AlterField(
        model_name='nominationmodel',
        name='work_name',
        field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, to='Forms.WorkModel', verbose_name='Work'),
    ),
    migrations.AlterField(
        model_name='pettycontractors',
        name='districts',
        field=models.CharField(max_length=50, null=True, verbose_name='District'),
    ),
    migrations.AlterField(
        model_name='pettycontractors',
        name='email',
        field=models.EmailField(blank=True, max_length=254, null=True, unique=True, verbose_name='Email ID'),
    ),
    migrations.AlterField(
        model_name='pettycontractors',
        name='first_name',
        field=models.CharField(default=0, max_length=100, verbose_name='First Name'),
        preserve_default=False,
    ),
    migrations.AlterField(
        model_name='pettycontractors',
        name='house_number',
        field=models.CharField(blank=True, max_length=50, null=True, verbose_name='Phone Number'),
    ),
    migrations.AlterField(
        model_name='pettycontractors',
        name='last_name',
        field=models.CharField(max_length=100, verbose_name='Last Name'),
    ),
    migrations.AlterField(
        model_name='pettycontractors',
        name='middle_name',
        field=models.CharField(blank=True, max_length=100, null=True, verbose_name='Middle Name'),
    ),
    migrations.AlterField(
        model_name='pettycontractors',
        name='pan_number',
        field=models.CharField(default=0, max_length=50, unique=True, verbose_name='PAN Number'),
        preserve_default=False,
    ),
    migrations.AlterField(
        model_name='pettycontractors',
        name='state',
        field=models.CharField(max_length=50, null=True, verbose_name='State'),
    ),
    migrations.AlterField(
        model_name='pettycontractors',
        name='street_name',
        field=models.CharField(max_length=50, null=True, verbose_name='Street Name'),
    ),
    migrations.AlterField(
        model_name='pettycontractors',
        name='tin_number',
        field=models.CharField(default=0, max_length=10, unique=True, verbose_name='TIN Number'),
        preserve_default=False,
    ),
    migrations.AlterField(
        model_name='projectmodel',
        name='created_on',
        field=models.DateTimeField(auto_now=True, verbose_name='Created On'),
    ),
    migrations.AlterField(
        model_name='projectmodel',
        name='financial_year',
        field=models.CharField(blank=True, max_length=5, verbose_name='Financial Year'),
    ),
    migrations.AlterField(
        model_name='projectmodel',
        name='project_name',
        field=models.CharField(blank=True, max_length=250, unique=True, verbose_name='Project Name'),
    ),
    migrations.AlterField(
        model_name='projectmodel',
        name='scheme_name',
        field=models.CharField(blank=True, max_length=250, verbose_name='Scheme Name'),
    ),
    migrations.AlterField(
        model_name='projectmodel',
        name='updated_on',
        field=models.DateTimeField(blank=True, null=True, verbose_name='Update On'),
    ),
    migrations.AlterField(
        model_name='revisedadministrativesanctionmodel',
        name='created_on',
        field=models.DateTimeField(auto_now=True, verbose_name='Created On'),
    ),
    migrations.AlterField(
        model_name='revisedadministrativesanctionmodel',
        name='project',
        field=models.ForeignKey(blank=True, on_delete=django.db.models.deletion.CASCADE, to='Forms.ProjectModel', verbose_name='Project'),
    ),
    migrations.AlterField(
        model_name='revisedadministrativesanctionmodel',
        name='updated_on',
        field=models.DateField(blank=True, default=None, null=True, verbose_name='Updated On'),
    ),
    migrations.AlterField(
        model_name='revisedtechnicalapprovalmodel',
        name='authority',
        field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, to='Forms.TechnicalAuthority', verbose_name='Technical Authority'),
    ),
    migrations.AlterField(
        model_name='revisedtechnicalapprovalmodel',
        name='created_on',
        field=models.DateTimeField(auto_now=True, verbose_name='Created On'),
    ),
    migrations.AlterField(
        model_name='revisedtechnicalapprovalmodel',
        name='updated_on',
        field=models.DateField(blank=True, default=None, null=True, verbose_name='Updated On'),
    ),
    migrations.AlterField(
        model_name='revisedtechnicalapprovalmodel',
        name='work_name',
        field=models.CharField(blank=True, max_length=300, verbose_name='Work'),
    ),
    migrations.AlterField(
        model_name='revisedtechnicalsanctionmodel',
        name='project',
        field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, to='Forms.ProjectModel', verbose_name='Project'),
    ),
    migrations.AlterField(
        model_name='schememodel',
        name='created_on',
        field=models.DateTimeField(auto_now=True, verbose_name='Created On'),
    ),
    migrations.AlterField(
        model_name='schememodel',
        name='dept_name',
        field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, to='Forms.UserDepartment', verbose_name='Department Name'),
    ),
    migrations.AlterField(
        model_name='schememodel',
        name='financial_year',
        field=models.CharField(blank=True, max_length=5, verbose_name='Financial Year'),
    ),
    migrations.AlterField(
        model_name='schememodel',
        name='scheme_name',
        field=models.CharField(max_length=250, unique=True, verbose_name='Scheme Name'),
    ),
    migrations.AlterField(
        model_name='schememodel',
        name='total_admin_sanction_amount',
        field=models.FloatField(verbose_name='Total Admin Sanction Amount'),
    ),
    migrations.AlterField(
        model_name='schememodel',
        name='updated_on',
        field=models.DateTimeField(blank=True, null=True, verbose_name='Updated On'),
    ),
    migrations.AlterField(
        model_name='technicalapprovalmodel',
        name='created_on',
        field=models.DateTimeField(auto_now=True, verbose_name='Created On'),
    ),
    migrations.AlterField(
        model_name='technicalapprovalmodel',
        name='financial_year',
        field=models.CharField(blank=True, max_length=5, verbose_name='Financial Year'),
    ),
    migrations.AlterField(
        model_name='technicalapprovalmodel',
        name='project_name',
        field=models.CharField(blank=True, max_length=250, verbose_name='Project Name'),
    ),
    migrations.AlterField(
        model_name='technicalapprovalmodel',
        name='updated_on',
        field=models.DateField(blank=True, null=True, verbose_name='Updated On'),
    ),
    migrations.AlterField(
        model_name='technicalapprovalmodel',
        name='work_name',
        field=models.CharField(blank=True, max_length=300, verbose_name='Work Name'),
    ),
    migrations.AlterField(
        model_name='technicalauthority',
        name='tender_authority',
        field=models.ForeignKey(default=0, on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL, verbose_name='Technical Authority'),
        preserve_default=False,
    ),
    migrations.AlterField(
        model_name='technicalsanctionmodel',
        name='created_on',
        field=models.DateTimeField(auto_now=True, verbose_name='Created On'),
    ),
    migrations.AlterField(
        model_name='technicalsanctionmodel',
        name='financial_year',
        field=models.CharField(blank=True, max_length=5, verbose_name='Financial Year'),
    ),
    migrations.AlterField(
        model_name='technicalsanctionmodel',
        name='project_name',
        field=models.CharField(blank=True, max_length=300, verbose_name='Project Name'),
    ),
    migrations.AlterField(
        model_name='technicalsanctionmodel',
        name='technical_authority',
        field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, to='Forms.TechnicalAuthority', verbose_name='Technical Authority'),
    ),
    migrations.AlterField(
        model_name='technicalsanctionmodel',
        name='updated_on',
        field=models.DateTimeField(blank=True, null=True, verbose_name='Update On'),
    ),
    migrations.AlterField(
        model_name='technicalwing',
        name='technical_wing_name',
        field=models.CharField(max_length=10, null=True, unique=True, verbose_name='Technical Wing'),
    ),
    migrations.AlterField(
        model_name='tender',
        name='created_on',
        field=models.DateTimeField(auto_now=True, verbose_name='Created On'),
    ),
    migrations.AlterField(
        model_name='tender',
        name='updated_on',
        field=models.DateTimeField(blank=True, null=True, verbose_name='Updated On'),
    ),
    migrations.AlterField(
        model_name='userdepartment',
        name='department_name',
        field=models.CharField(max_length=50, null=True, unique=True, verbose_name='Department Name'),
    ),
    migrations.AlterField(
        model_name='userdepartment',
        name='department_reference',
        field=models.CharField(max_length=7, null=True, unique=True, verbose_name='Department Name'),
    ),
    migrations.AlterField(
        model_name='workmodel',
        name='created_on',
        field=models.DateTimeField(auto_now=True, verbose_name='Created On'),
    ),
    migrations.AlterField(
        model_name='workmodel',
        name='financial_year',
        field=models.CharField(blank=True, max_length=5, verbose_name='Financial Year'),
    ),
    migrations.AlterField(
        model_name='workmodel',
        name='project_name',
        field=models.CharField(blank=True, max_length=250, verbose_name='Project Name'),
    ),
    migrations.AlterField(
        model_name='workmodel',
        name='updated_on',
        field=models.DateTimeField(blank=True, null=True, verbose_name='Updated On'),
    ),
    migrations.AlterField(
        model_name='workmodel',
        name='work_name',
        field=models.CharField(blank=True, max_length=300, unique=True, verbose_name='Work Name'),
    ),
    migrations.AlterField(
        model_name='workordermodel',
        name='created_on',
        field=models.DateTimeField(auto_now=True, verbose_name='Created On'),
    ),
    migrations.AlterField(
        model_name='workordermodel',
        name='tender_authority',
        field=models.ForeignKey(blank=True, on_delete=django.db.models.deletion.CASCADE, to='Forms.TechnicalAuthority', verbose_name='Technical Authority'),
    ),
    migrations.AlterField(
        model_name='workordermodel',
        name='updated_on',
        field=models.DateTimeField(blank=True, null=True, verbose_name='Updated On'),
    ),
    migrations.AlterField(
        model_name='workordermodel',
        name='work_name',
        field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, to='Forms.WorkModel', verbose_name='Work'),
    ),
]

1 个答案:

答案 0 :(得分:2)

您似乎缺少添加用户模型的迁移的依赖项。我不确定为什么它会丢失,但请尝试手动添加它并再次运行迁移:

dependencies = [
    migrations.swappable_dependency(settings.AUTH_USER_MODEL),
    ('Forms', '0009_revised_tender_changes'),
]