From the online examples, this piece of code should create a model class that I can migrate. However, when ever I try to migrate, it keeps on telling me "AttributeError: type object 'Foo' has no attribute 'REQUIRED_FIELDS', or "USERNAME_FIELD" , and then keep on going. I don't understand why it makes me create these fields but in tutorials the code works without them.
from django.db import models
class Foo(models.Model):
pass
The Tutorial: https://www.w3schools.com/django/django_models.php
Error:
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'
Solution: From @Rahul K P in the comments below: It's due to that, If you want Foo as your auth model it should be an instance of AbstractBaseUser . So, I would suggest removing that it should work. or from django.contrib.auth.models import AbstractBaseUser; class Foo(AbstractBaseUser):
CodePudding user response:
It Django model should have at least one field.
from django.db import models
class Foo(models.Model):
REQUIRED_FIELDS = ('user',)
test = models.CharField(max_length=30)