Skip to main content
 首页 » 编程设计

python之以 10 为基数的 int() 的 Django 无效文字 : ' ' when i try to migrate

2025年05月04日26luoye11

我正在尝试在添加到我的项目的新应用程序中创建一个新表。makemigrations 工作得很好,但 migrate 不工作。这是我的模型

博客/模型.py

from django.db import models 
 
# Create your models here. 
from fostania_web_app.models import UserModel 
 
 
class Tag(models.Model): 
    tag_name = models.CharField(max_length=250) 
 
    def __str__(self): 
        return self.tag_name 
 
 
class BlogPost(models.Model): 
    post_title = models.CharField(max_length=250) 
    post_message = models.CharField(max_length=2000) 
    post_author = models.ForeignKey(UserModel, on_delete=models.PROTECT) 
    post_image = models.ImageField(upload_to='documents/%Y/%m/%d', null=False, blank=False) 
    post_tag = models.ForeignKey(Tag, on_delete=models.PROTECT) 
    post_created_at = models.DateTimeField(auto_now=True) 

当我尝试执行 python manage.py migrate 时出现此错误

以 10 为底的 int() 的无效文字:'???????? ????????'

UserModel 是在同一项目的另一个应用程序中创建的,这就是为什么我使用语句 from fostania_web_app.models import UserModel

fostania_web_app/models.py

class UserModelManager(BaseUserManager): 
    def create_user(self, email, password, pseudo): 
        user = self.model() 
        user.name = name 
        user.email = self.normalize_email(email=email) 
        user.set_password(password) 
        user.save() 
 
        return user 
 
    def create_superuser(self, email, password): 
        ''' 
        Used for: python manage.py createsuperuser 
        ''' 
        user = self.model() 
        user.name = 'admin-yeah' 
        user.email = self.normalize_email(email=email) 
        user.set_password(password) 
 
        user.is_staff = True 
        user.is_superuser = True 
        user.save() 
 
        return user 
 
 
class UserModel(AbstractBaseUser, PermissionsMixin): 
    ## Personnal fields. 
    email = models.EmailField(max_length=254, unique=True) 
    name = models.CharField(max_length=16) 
    ## [...] 
 
    ## Django manage fields. 
    date_joined = models.DateTimeField(auto_now_add=True) 
    is_active = models.BooleanField(default=True) 
    is_staff = models.BooleanField(default=False) 
 
    USERNAME_FIELD = 'email' 
    REQUIRED_FIELD = ['email', 'name'] 
 
    objects = UserModelManager() 
 
    def __str__(self): 
        return self.email 
 
    def get_short_name(self): 
        return self.name[:2].upper() 
 
    def get_full_name(self): 
        return self.name 

在我的 setting.py 文件中我有这个:

#custom_user 
AUTH_USER_MODEL='fostania_web_app.UserModel' 

这是完整的traeback:

Operations to perform: 
  Apply all migrations: admin, auth, blog, contenttypes, fostania_web 
ons, sites, social_django 
Running migrations: 
  Applying blog.0001_initial...Traceback (most recent call last): 
  File "manage.py", line 15, in <module> 
    execute_from_command_line(sys.argv) 
  File "C:\Users\LiTo\AppData\Local\Programs\Python\Python36-32\lib\s 
s\django\core\management\__init__.py", line 371, in execute_from_comm 
    utility.execute() 
  File "C:\Users\LiTo\AppData\Local\Programs\Python\Python36-32\lib\s 
s\django\core\management\__init__.py", line 365, in execute 
    self.fetch_command(subcommand).run_from_argv(self.argv) 
  File "C:\Users\LiTo\AppData\Local\Programs\Python\Python36-32\lib\s 
s\django\core\management\base.py", line 288, in run_from_argv 
    self.execute(*args, **cmd_options) 
  File "C:\Users\LiTo\AppData\Local\Programs\Python\Python36-32\lib\s 
s\django\core\management\base.py", line 335, in execute 
    output = self.handle(*args, **options) 
  File "C:\Users\LiTo\AppData\Local\Programs\Python\Python36-32\lib\s 
s\django\core\management\commands\migrate.py", line 200, in handle 
    fake_initial=fake_initial, 
  File "C:\Users\LiTo\AppData\Local\Programs\Python\Python36-32\lib\s 
s\django\db\migrations\executor.py", line 117, in migrate 
    state = self._migrate_all_forwards(state, plan, full_plan, fake=f 
nitial=fake_initial) 
  File "C:\Users\LiTo\AppData\Local\Programs\Python\Python36-32\lib\s 
s\django\db\migrations\executor.py", line 147, in _migrate_all_forwar 
    state = self.apply_migration(state, migration, fake=fake, fake_in 
initial) 
  File "C:\Users\LiTo\AppData\Local\Programs\Python\Python36-32\lib\s 
s\django\db\migrations\executor.py", line 244, in apply_migration 
    state = migration.apply(state, schema_editor) 
  File "C:\Users\LiTo\AppData\Local\Programs\Python\Python36-32\lib\s 
s\django\db\migrations\migration.py", line 122, in apply 
    operation.database_forwards(self.app_label, schema_editor, old_st 
t_state) 
  File "C:\Users\LiTo\AppData\Local\Programs\Python\Python36-32\lib\s 
s\django\db\migrations\operations\fields.py", line 84, in database_fo 
    field, 
  File "C:\Users\LiTo\AppData\Local\Programs\Python\Python36-32\lib\s 
s\django\db\backends\sqlite3\schema.py", line 306, in add_field 
    self._remake_table(model, create_field=field) 
  File "C:\Users\LiTo\AppData\Local\Programs\Python\Python36-32\lib\s 
s\django\db\backends\sqlite3\schema.py", line 178, in _remake_table 
    self.effective_default(create_field) 
  File "C:\Users\LiTo\AppData\Local\Programs\Python\Python36-32\lib\s 
s\django\db\backends\base\schema.py", line 240, in effective_default 
    default = field.get_db_prep_save(default, self.connection) 
  File "C:\Users\LiTo\AppData\Local\Programs\Python\Python36-32\lib\s 
s\django\db\models\fields\related.py", line 936, in get_db_prep_save 
    return self.target_field.get_db_prep_save(value, connection=conne 
  File "C:\Users\LiTo\AppData\Local\Programs\Python\Python36-32\lib\s 
s\django\db\models\fields\__init__.py", line 767, in get_db_prep_save 
    return self.get_db_prep_value(value, connection=connection, prepa 
  File "C:\Users\LiTo\AppData\Local\Programs\Python\Python36-32\lib\s 
s\django\db\models\fields\__init__.py", line 939, in get_db_prep_valu 
    value = self.get_prep_value(value) 
  File "C:\Users\LiTo\AppData\Local\Programs\Python\Python36-32\lib\s 
s\django\db\models\fields\__init__.py", line 947, in get_prep_value 
    return int(value) 
ValueError: invalid literal for int() with base 10: '??? ??? ?????' 

请您参考如下方法:

错误消息:ValueError:以 10 为底的 int() 的无效文字:'??? ??? ????'

根据以 10 为底的 int() 异常:'??? ??? ??????' 不符合 int 的条件。 为 '??? 检查 blog.0001_initial 迁移??? ??????' 并使用有效的 int 修改该值。

您可能在重新运行不是 int() 的 makemigrations 命令时不小心提供了垃圾默认值