خوب اول باید یک مدل بسازی و اونو به جنگو معرفی کنی مثل کد زیر :
from django.contrib.auth.models import AbstractUser
class CustomUser(AbstractUser):
mobile = models.CharField(max_length=16)
بعدش اینجوری به جنگو معرفی می کنی :
AUTH_USER_MODEL = 'myapp.CustomUser'
البته لازم نیست فیلد های یوزنیم و پسورد رو اضافه کنی چون ما داریم با این کار یک بازنویسی از مدل اصلی انجام می دیم
@mahdisafarydev
خیلی ممنونم از شما،
من یک اپ درست کردم به اسم target، در قسمت models به این صورت :
from django.db import models
from django.contrib.auth.models import AbstractUser
# Create your models here.
class CustomUser(AbstractUser):
user_type = models.IntegerField( default = 2 )
بعد تو قسمت settings هم این خط رو اضافه کردم:
AUTH_USER_MODEL = 'target.CustomUser'
وقتی دستور:
py manage.py migrate
اجرا میکنم با این ارور مواجه میشم:
System check identified some issues:
WARNINGS:
?: (mysql.W002) MariaDB Strict Mode is not set for database connection 'default'
HINT: MariaDB's Strict Mode fixes many data integrity problems in MariaDB, such as data truncation upon insertion, by escalating warnings into errors. It is strongly recommended you activate it. See: https://docs.djangoproject.com/en/3.2/ref/databases/#mysql-sql-mode
Operations to perform:
Apply all migrations: admin, auth, contenttypes, sessions, target
Traceback (most recent call last):
File "C:\Users\Salar_System\Desktop\scrap_news\scrap\manage.py", line 22, in <module>
main()
File "C:\Users\Salar_System\Desktop\scrap_news\scrap\manage.py", line 18, in main
execute_from_command_line(sys.argv)
File "C:\Users\Salar_System\Desktop\scrap_news\venv\lib\site-packages\django\core\management\__init__.py", line 419, in execute_from_command_line
utility.execute()
File "C:\Users\Salar_System\Desktop\scrap_news\venv\lib\site-packages\django\core\management\__init__.py", line 413, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\Users\Salar_System\Desktop\scrap_news\venv\lib\site-packages\django\core\management\base.py", line 354, in run_from_argv
self.execute(*args, **cmd_options)
File "C:\Users\Salar_System\Desktop\scrap_news\venv\lib\site-packages\django\core\management\base.py", line 398, in execute
output = self.handle(*args, **options)
File "C:\Users\Salar_System\Desktop\scrap_news\venv\lib\site-packages\django\core\management\base.py", line 89, in wrapped
res = handle_func(*args, **kwargs)
File "C:\Users\Salar_System\Desktop\scrap_news\venv\lib\site-packages\django\core\management\commands\migrate.py", line 202, in handle
pre_migrate_apps = pre_migrate_state.apps
File "C:\Users\Salar_System\Desktop\scrap_news\venv\lib\site-packages\django\utils\functional.py", line 48, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "C:\Users\Salar_System\Desktop\scrap_news\venv\lib\site-packages\django\db\migrations\state.py", line 208, in apps
return StateApps(self.real_apps, self.models)
File "C:\Users\Salar_System\Desktop\scrap_news\venv\lib\site-packages\django\db\migrations\state.py", line 277, in __init__
raise ValueError("\n".join(error.msg for error in errors))
ValueError: The field admin.LogEntry.user was declared with a lazy reference to 'target.customuser', but app 'target' isn't installed.
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'OPTIONS': {
'sql_mode': 'traditional',
}
}
}
یه همچین چیزی باید توی صفحه تنظیمات باشه که می تونی مود SQL رو اینجوری ست کنی
@mahdisafarydev
این مورد رو انجام دادم با این ارور مواجه شدم:
Operations to perform:
Apply all migrations: admin, auth, contenttypes, sessions, target
Traceback (most recent call last):
File "C:\Users\Salar_System\Desktop\scrap_news\scrap\manage.py", line 22, in <module>
main()
File "C:\Users\Salar_System\Desktop\scrap_news\scrap\manage.py", line 18, in main
execute_from_command_line(sys.argv)
File "C:\Users\Salar_System\Desktop\scrap_news\venv\lib\site-packages\django\core\management\__init__.py", line 419, in execute_from_command_line
utility.execute()
File "C:\Users\Salar_System\Desktop\scrap_news\venv\lib\site-packages\django\core\management\__init__.py", line 413, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\Users\Salar_System\Desktop\scrap_news\venv\lib\site-packages\django\core\management\base.py", line 354, in run_from_argv
self.execute(*args, **cmd_options)
File "C:\Users\Salar_System\Desktop\scrap_news\venv\lib\site-packages\django\core\management\base.py", line 398, in execute
output = self.handle(*args, **options)
File "C:\Users\Salar_System\Desktop\scrap_news\venv\lib\site-packages\django\core\management\base.py", line 89, in wrapped
res = handle_func(*args, **kwargs)
File "C:\Users\Salar_System\Desktop\scrap_news\venv\lib\site-packages\django\core\management\commands\migrate.py", line 202, in handle
pre_migrate_apps = pre_migrate_state.apps
File "C:\Users\Salar_System\Desktop\scrap_news\venv\lib\site-packages\django\utils\functional.py", line 48, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "C:\Users\Salar_System\Desktop\scrap_news\venv\lib\site-packages\django\db\migrations\state.py", line 208, in apps
return StateApps(self.real_apps, self.models)
File "C:\Users\Salar_System\Desktop\scrap_news\venv\lib\site-packages\django\db\migrations\state.py", line 277, in __init__
raise ValueError("\n".join(error.msg for error in errors))
ValueError: The field admin.LogEntry.user was declared with a lazy reference to 'target.customuser', but app 'target' isn't installed.
با اینکه داخل فایل setting من اینا رو وارد کردم:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'target'
]
AUTH_USER_MODEL = 'target.CustomUser'
میگه دوباره باید بسازی توصیه من این که یه بار تیبل ها رو کامل پاک کن و همینطور مایگریشن هاتو بعدش برو دوباره مایگریشن هاتو بساز و اونارو توی دیتابیس اعمال کن
@mahdisafarydev
خیلی ممنونم
یه چیزی هم هست من این فیلدی که ایجاد کردم رو میخوام وقتی دارم superuser درست میکنم مقدار خاصی بگیره این و چطوری باید ست کنم؟
آیا مایل به ارسال نوتیفیکیشن و اخبار از طرف راکت هستید ؟