简体   繁体   English

Django 模型中的错误从何而来?

[英]Where did the error in the Django model come from?

I'm trying to add additional fields for the User model.我正在尝试为 User 模型添加其他字段。 The migrate and makemigrations commands worked. migrate 和 makemigrations 命令有效。 When I try to create createsuperuser, this error occurs .当我尝试创建 createsuperuser 时,会发生此错误。 Postgresql Is Enabled . Postgresql 已启用。 I tried to change upload_to, add "blank = True", "default" to the model parameters, but nothing changed.我尝试更改upload_to,在模型参数中添加“blank = True”、“default”,但没有任何改变。

Trace:痕迹:

  return self.cursor.execute(sql, params)
psycopg2.errors.UndefinedTable: ОШИБКА:  отношение "blog_customuser" не существует
LINE 1: ..._customuser"."age", "blog_customuser"."city" FROM "blog_cust...
                                                             ^


The above exception was the direct cause of the following exception:

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 "C:\Users\Роман\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\management\__init__.py", line 381, in execute_from_command_line
    utility.execute()
  File "C:\Users\Роман\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\management\__init__.py", line 375, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\Users\Роман\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\management\base.py", line 323, in run_from_argv
    self.execute(*args, **cmd_options)
  File "C:\Users\Роман\AppData\Local\Programs\Python\Python37\lib\site-packages\django\contrib\auth\management\commands\createsuperuser.py", line 61, in execute
    return super().execute(*args, **options)
  File "C:\Users\Роман\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\management\base.py", line 364, in execute
    output = self.handle(*args, **options)
  File "C:\Users\Роман\AppData\Local\Programs\Python\Python37\lib\site-packages\django\contrib\auth\management\commands\createsuperuser.py", line 95, in handle
    error_msg = self._validate_username(username, verbose_field_name, database)
  File "C:\Users\Роман\AppData\Local\Programs\Python\Python37\lib\site-packages\django\contrib\auth\management\commands\createsuperuser.py", line 201, in _validate_username
    self.UserModel._default_manager.db_manager(database).get_by_natural_key(username)
  File "C:\Users\Роман\AppData\Local\Programs\Python\Python37\lib\site-packages\django\contrib\auth\base_user.py", line 44, in get_by_natural_key
    return self.get(**{self.model.USERNAME_FIELD: username})
  File "C:\Users\Роман\AppData\Local\Programs\Python\Python37\lib\site-packages\django\db\models\manager.py", line 82, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "C:\Users\Роман\AppData\Local\Programs\Python\Python37\lib\site-packages\django\db\models\query.py", line 402, in get
    num = len(clone)
  File "C:\Users\Роман\AppData\Local\Programs\Python\Python37\lib\site-packages\django\db\models\query.py", line 256, in __len__
    self._fetch_all()
  File "C:\Users\Роман\AppData\Local\Programs\Python\Python37\lib\site-packages\django\db\models\query.py", line 1242, in _fetch_all
    self._result_cache = list(self._iterable_class(self))
  File "C:\Users\Роман\AppData\Local\Programs\Python\Python37\lib\site-packages\django\db\models\query.py", line 55, in __iter__
    results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size)
  File "C:\Users\Роман\AppData\Local\Programs\Python\Python37\lib\site-packages\django\db\models\sql\compiler.py", line 1097, in execute_sql
    cursor.execute(sql, params)
  File "C:\Users\Роман\AppData\Local\Programs\Python\Python37\lib\site-packages\django\db\backends\utils.py", line 99, in execute
    return super().execute(sql, params)
  File "C:\Users\Роман\AppData\Local\Programs\Python\Python37\lib\site-packages\django\db\backends\utils.py", line 67, in execute
    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
  File "C:\Users\Роман\AppData\Local\Programs\Python\Python37\lib\site-packages\django\db\backends\utils.py", line 76, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File "C:\Users\Роман\AppData\Local\Programs\Python\Python37\lib\site-packages\django\db\backends\utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
  File "C:\Users\Роман\AppData\Local\Programs\Python\Python37\lib\site-packages\django\db\utils.py", line 89, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "C:\Users\Роман\AppData\Local\Programs\Python\Python37\lib\site-packages\django\db\backends\utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: ОШИБКА:  отношение "blog_customuser" не существует
LINE 1: ..._customuser"."age", "blog_customuser"."city" FROM "blog_cust...

Model:模型:

from django.db import models
from django.contrib.auth.models import AbstractUser

class User(AbstractUser):

    avatar = models.ImageField(upload_to='images/%Y/%m/%d', blank=True, max_length=1000)
    age = models.CharField(blank=True,max_length=200)

class Post(models.Model):
    title = models.CharField(max_length=100,blank=True,db_index=True)
    body = models.TextField(max_length=500,blank=True,db_index=True)
    date_pub = models.DateTimeField(auto_now_add=True)
    author = models.ForeignKey(User, related_name='posts',on_delete=models.PROTECT)

你的年龄字段应该是这样的:

age = models.PositiveIntegerField(null = True, blank = True)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM