lazy reference: doesn't provide model user?

后端 未结 9 1324
独厮守ぢ
独厮守ぢ 2020-12-15 06:33

Currently I\'m using Django 1.11 and Python 3.6. I\'m attempting to create a custom user model with a new application that authenticates with LDAP, but i\'m greeted with th

相关标签:
9条回答
  • 2020-12-15 06:53

    Remove all the migrations under Migration Directory

    Now you can use makemigrations and migrate

    0 讨论(0)
  • 2020-12-15 06:54

    Apparently nothing was incorrect with the code, i just had to drop all my user tables and run makemigration and migrate.

    0 讨论(0)
  • 2020-12-15 06:56

    Warning: It will delete your whole database. If you have some important data then backup it by dumpdata and then restore it by loaddata. for more info check here (I am not sure about this).

    It is very difficult to change AUTH_USER_MODEL in the middle of the project. See the note in the docs. Infect after you are done with your first migration of Django tables, you will face problems.

    Idea is: You need to include your custom user model with its entry in setting.py (AUTH_USER_MODEL = [custom user model] ) in your first-ever migration (where django create its own table like auth_group, dajango_migrations etc... ) of Django project.

    Warning: If you have started server then Django itself create database and then this will not work, so please do not start the server.

    • Delete dbsqlite3 database file, delete all your migrations files and its binary files (In app/migration/pycache ) except init file and comment out all your models and it's dependencies (like, you have used model in any form then comment it also).
    • Now uncomment only custom user model
    • add AUTH_USER_MODEL in setting.py (AUTH_USER_MODEL = [custom user model] )
    • and then run makemigrations and migrate. (this will create your all Django tables and your custom user model.)

    It is done.

    now you can start the server.

    after that uncomment all other models and migrate them.

    0 讨论(0)
  • 2020-12-15 06:59

    This is caused by your settings.AUTH_USER_MODEL having changed to a model that does not exist when migrations are being computed.

    A slightly hacky way to fix this without data loss, if you're migrating from auth.User to custom.User is to add a "virtual" (separate database and state) minimal model (that is, only the ID field, to allow for foreign keys) according to the new User model in the very initial migration, so future migrations have this reference:

    operations=[
        migrations.SeparateDatabaseAndState(
            state_operations=[
                migrations.CreateModel(
                    name="User",
                    fields=[
                        (
                            "id",
                            models.AutoField(
                                auto_created=True,
                                primary_key=True,
                                serialize=False,
                                verbose_name="ID",
                            ),
                        )
                    ],
                    options={"db_table": "auth_user"},
                    managers=[("objects", UserManager())],
                )
            ]
        ),
        # ... other migrations
    
    0 讨论(0)
  • 2020-12-15 07:00

    user=models.OneToOneField(User,on_delete=models.PROTECT)

    0 讨论(0)
  • 2020-12-15 07:04

    WARNING: It will destroy Your current User/Group/Auth tables and entries connected with User model

    Actually in django 1.9+ this is enough:

    • drop all auth_* and django_admin_log tables using the following statement :

    DROP TABLE django_admin_log, auth_group, auth_group_permissions, auth_permission, auth_user, auth_user_groups, auth_user_user_permissions CASCADE;

    • delete all migrations connected with django_admin and auth apps with:

    DELETE FROM django_migrations WHERE app='admin' or app='auth';

    • Then simply run:

    ./manage.py migrate

    0 讨论(0)
提交回复
热议问题