6

I am trying to make custom made user model for my project in Django.

My models.py:

class myCustomeUser(AbstractUser):
    id = models.AutoField(primary_key=True)
    username = models.CharField(max_length=20, unique="True", blank=False)
    password = models.CharField(max_length=20, blank=False)
    is_Employee = models.BooleanField(default=False)
    is_Inspector = models.BooleanField(default=False)
    is_Industry = models.BooleanField(default=False)
    is_Admin = models.BooleanField(default=False)


class Industry(models.Model):
    user = models.OneToOneField(myCustomeUser, on_delete=models.CASCADE, primary_key=True, related_name='industry_releted_user')
    name = models.CharField(max_length=200, blank=True)
    owner = models.CharField(max_length=200, blank=True)
    license = models.IntegerField(null=True, unique=True)
    industry_extrafield = models.TextField(blank=True)


class Employee(models.Model):
    user = models.OneToOneField(myCustomeUser, on_delete=models.CASCADE, primary_key=True, related_name='employee_releted_user')
    industry = models.OneToOneField(Industry, on_delete=models.CASCADE, related_name='employee_releted_industry')
    i_id = models.IntegerField(null=True, blank=False, unique=True)
    name = models.CharField(max_length=200, blank=False, null=True)
    gmail = models.EmailField(null=True, blank=False, unique=True)
    rank = models.CharField(max_length=20, blank=False, null=True)
    employee_varified = models.BooleanField(default=False)


class Inspector(models.Model):
    user = models.OneToOneField(myCustomeUser, on_delete=models.CASCADE, primary_key=True, related_name='inspector_releted_user')
    inspector_extrafield = models.TextField(blank=True)


class Admin(models.Model):
    user = models.OneToOneField(myCustomeUser, on_delete=models.CASCADE, primary_key=True, related_name='admin_releted_user')
    admin_extrafield = models.TextField(blank=True)

in settings.py:

AUTH_USER_MODEL = 'app.myCustomeUser'

Here admin.site.register is also done in admin.py. Now it shows the following message in the terminal while I try to migrate or makemigrations:

Traceback (most recent call last):
  File "manage.py", line 21, in <module>
    main()
  File "manage.py", line 17, in main
    execute_from_command_line(sys.argv)
  File "G:\Python\lib\site-packages\django\core\management\__init__.py", line 401, in execute_from_command_line   
    utility.execute()
  File "G:\Python\lib\site-packages\django\core\management\__init__.py", line 395, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "G:\Python\lib\site-packages\django\core\management\base.py", line 328, in run_from_argv
    self.execute(*args, **cmd_options)
  File "G:\Python\lib\site-packages\django\core\management\base.py", line 369, in execute
    output = self.handle(*args, **options)
  File "G:\Python\lib\site-packages\django\core\management\base.py", line 83, in wrapped
    res = handle_func(*args, **kwargs)
  File "G:\Python\lib\site-packages\django\core\management\commands\makemigrations.py", line 101, in handle       
    loader.check_consistent_history(connection)
  File "G:\Python\lib\site-packages\django\db\migrations\loader.py", line 295, in check_consistent_history        
    raise InconsistentMigrationHistory(
django.db.migrations.exceptions.InconsistentMigrationHistory: Migration admin.0001_initial is applied before its dependency app.0001_initial on database 'default'.

What does it mean? And I also don't want to set the default value of username & password in this myCustomeUser model. And also please suggest me that, is this a correct way to make usermodel?

Kanchon Gharami
  • 777
  • 1
  • 11
  • 30
  • https://docs.djangoproject.com/en/3.1/topics/auth/customizing/#changing-to-a-custom-user-model-mid-project – iklinac Jan 04 '21 at 13:25
  • Here I also try to `migrate` after dropping my full database, but it doesn't work. How I can solve this problem? – Kanchon Gharami Jan 04 '21 at 13:39
  • You will have to delete and remake migrations, just migrating again would not solve issue as it is documented – iklinac Jan 04 '21 at 13:43
  • 1
    Does this answer your question? [Migrating from django user model to a custom user model](https://stackoverflow.com/questions/42794212/migrating-from-django-user-model-to-a-custom-user-model) – iklinac Jan 04 '21 at 13:44
  • 1
    @iklinac I have just run `python manage.py migrate admin zero`, `python manage.py migrate auth zero`, `python manage.py migrate contenttypes zero`, `python manage.py migrate sessions zero` to delete my migrations and then re `makemigrations` and `migrate`, but still that problem stay. – Kanchon Gharami Jan 04 '21 at 14:25

3 Answers3

7

This error will usually happen if you've done your first initial migration without including your custom user model migration file. Exactly as the message says:

"admin.0001_initial is applied before its dependency custom_user_app_label.0001_initial on database 'default'"

Since beginners will always do their initial migration and then create a custom user afterward. In the first migration, it will migrate all built-in Django apps including admin. Now with the custom user model, Django wanted it to be the first initial migration to be executed.

See Django docs Changing to a custom user model mid-project.

Due to limitations of Django’s dynamic dependency feature for swappable models, the model referenced by AUTH_USER_MODEL must be created in the first migration of its app (usually called 0001_initial); otherwise, you’ll have dependency issues.


In my case, I have Django v4.1 installed.

This is how I reproduce the issue:

  • I had initially migrated my application previously. Then
  • Created a custom User.
  • Added the app into INSTALLED_APPS.
  • Created migration files python manage.py makemigrations
  • Set AUTH_USER_MODEL to the new custom User by AUTH_USER_MODEL = 'myapp_label.User'
  • Got the migration error issue as the OP.

This is how I resolve the issue:

  • Drop the database but DO NOT delete the User initial migration.

  • Make sure in your 0001_initial.py -> class Migration.initial is set to True.

  • Run migration python manage.py migrate

  • Your User initial migration should be the first migration file in the order of migration execution. See this migration output as example:

    Running migrations:
     Applying myapp_label.0001_initial... OK
     Applying contenttypes.0001_initial... OK
     Applying admin.0001_initial...
    

As you can see it was executed first followed by the contenttypes then the admin.

Shift 'n Tab
  • 8,808
  • 12
  • 73
  • 117
3

I also had the same problem, but I just deleted my migrations from every app, and drop the database, and created a new one. And use `

python manage.py makemigrations python manage.py migrate

to make new migrations and everything is working as it should be.

user14512746
  • 83
  • 1
  • 2
0
  1. Comment path('admin/', admin.site.urls) in project urls.py file.
  2. Then in settings.py file comment django.contrib.admin in installed app.
  3. Do makemigrations and migrate it new table with your customize fields for user is created in table.
  4. You can comment out above after the migration, it will work too.
doneforaiur
  • 1,308
  • 7
  • 14
  • 21