從在線示例中,這段代碼應該創建一個我可以遷移的模型類。但是,當我嘗試遷移時,它一直告訴我“AttributeError: type object 'Foo' has no attribute 'REQUIRED_FIELDS', or "USERNAME_FIELD" ,然后繼續。我不明白為什么它讓我創建這些欄位,但在教程中,代碼在沒有它們的情況下作業。
from django.db import models
class Foo(models.Model):
pass
教程:https ://www.w3schools.com/django/django_models.php
錯誤:
python3 manage.py makemigrations things
Traceback (most recent call last):
File "/home/kofkbuntu/uni/year2/5CCS2SEG/week2/ThingsApp1/things-1-jnile/manage.py", line 22, in <module>
main()
File "/home/kofkbuntu/uni/year2/5CCS2SEG/week2/ThingsApp1/things-1-jnile/manage.py", line 18, in main
execute_from_command_line(sys.argv)
File "/home/kofkbuntu/uni/year2/5CCS2SEG/week2/ThingsApp1/things-1-jnile/venv/lib/python3.10/site-packages/django/core/management/__init__.py", line 446, in execute_from_command_line
utility.execute()
File "/home/kofkbuntu/uni/year2/5CCS2SEG/week2/ThingsApp1/things-1-jnile/venv/lib/python3.10/site-packages/django/core/management/__init__.py", line 440, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/kofkbuntu/uni/year2/5CCS2SEG/week2/ThingsApp1/things-1-jnile/venv/lib/python3.10/site-packages/django/core/management/base.py", line 402, in run_from_argv
self.execute(*args, **cmd_options)
File "/home/kofkbuntu/uni/year2/5CCS2SEG/week2/ThingsApp1/things-1-jnile/venv/lib/python3.10/site-packages/django/core/management/base.py", line 443, in execute
self.check()
File "/home/kofkbuntu/uni/year2/5CCS2SEG/week2/ThingsApp1/things-1-jnile/venv/lib/python3.10/site-packages/django/core/management/base.py", line 475, in check
all_issues = checks.run_checks(
File "/home/kofkbuntu/uni/year2/5CCS2SEG/week2/ThingsApp1/things-1-jnile/venv/lib/python3.10/site-packages/django/core/checks/registry.py", line 88, in run_checks
new_errors = check(app_configs=app_configs, databases=databases)
File "/home/kofkbuntu/uni/year2/5CCS2SEG/week2/ThingsApp1/things-1-jnile/venv/lib/python3.10/site-packages/django/contrib/auth/checks.py", line 29, in check_user_model
if not isinstance(cls.REQUIRED_FIELDS, (list, tuple)):
AttributeError: type object 'Foo' has no attribute 'REQUIRED_FIELDS'
解決方案: 來自@Rahul KP 在下面的評論中:正因為如此,如果您希望 Foo 作為您的身份驗證模型,它應該是 AbstractBaseUser 的一個實體。所以,我建議洗掉它應該可以作業。或從 django.contrib.auth.models 匯入 AbstractBaseUser;類Foo(AbstractBaseUser):
uj5u.com熱心網友回復:
它的 Django 模型應該至少有一個欄位。
from django.db import models
class Foo(models.Model):
REQUIRED_FIELDS = ('user',)
test = models.CharField(max_length=30)
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/514842.html
