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
?
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:
User
.INSTALLED_APPS
.python manage.py makemigrations
AUTH_USER_MODEL
to the new custom User
by AUTH_USER_MODEL = 'myapp_label.User'
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.