ValueError:字段“id”需要一个数字但得到“处理”

srv*_*aqa 4 python django

我将在 DigitalOcean 上部署我的 django 应用程序。一切顺利,除了以下错误,我的问题是:我在哪里可以找到这个错误的来源,实际上是在哪个文件中?

Operations to perform:
  Apply all migrations: admin, auth, ccapp, contenttypes, sessions
Running migrations:
  Applying ccapp.0009_auto_20191207_2148...Traceback (most recent call last):
  File "/home/progbash/ccproject/env/lib/python3.6/site-packages/django/db/models/fields/__init__.py", line 1768, in get_prep_value
    return int(value)

ValueError: invalid literal for int() with base 10: 'Processing'

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 "/home/progbash/ccproject/env/lib/python3.6/site-packages/django/core/management/__init__.py", line 401, in execute_from_command_line
    utility.execute()
  File "/home/progbash/ccproject/env/lib/python3.6/site-packages/django/core/management/__init__.py", line 395, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
...
  File "/home/progbash/ccproject/env/lib/python3.6/site-packages/django/db/models/fields/__init__.py", line 2361, in get_db_prep_value
    value = self.get_prep_value(value)
  File "/home/progbash/ccproject/env/lib/python3.6/site-packages/django/db/models/fields/__init__.py", line 1772, in get_prep_value
    ) from e
ValueError: Field 'id' expected a number but got 'Processing'.
Run Code Online (Sandbox Code Playgroud)

models.py

from datetime import datetime

# Create your models here.
class Question(models.Model):
    question_text = models.TextField(max_length=200)
    answer = models.TextField(max_length=200)

    def __str__(self):
        return self.question_text

class ApplicantStatus(models.Model):
    class Meta:
        verbose_name_plural = "Applicant Statuses"

    name = models.CharField(max_length=30)

    def __str__(self):
        return self.name

class Applicant(models.Model):
    name = models.CharField(max_length=20)
    surname = models.CharField(max_length=30)
    birth_date = models.DateField(blank=False)
    phone = models.CharField(max_length=15)
    email = models.EmailField(max_length=40)
    motivation_letter = models.TextField(max_length=200)
    status = models.ForeignKey(ApplicantStatus, on_delete=models.CASCADE, default=3)
    photo = models.FileField(upload_to='static/applicant_photos', blank=True)

    def __str__(self):
        return self.name


class Message(models.Model):
    message_text = models.CharField(max_length=200)
    sender_name = models.CharField(max_length=30)
    sender_email = models.EmailField(max_length=50)

    def __str__(self):
        return self.sender_name
Run Code Online (Sandbox Code Playgroud)

小智 15

只是为了分享处理我收到的类似错误的解决方案:在我的例子中,收到了同样的错误,因为我直接使用字段值创建模型实例,而不指定字段名称,所以 ID 总是采用第一个为默认值(ID=field1)。通过将属性名称也添加到即时创建中解决了这个问题。

曾是:

model_instant = YourModel(field1, field2,...etc)
Run Code Online (Sandbox Code Playgroud)

解决方法:

model_instant = YourModel(field1 = field1, field2 = field2,...etc)
Run Code Online (Sandbox Code Playgroud)

执行此操作,然后按照上面建议的操作:1)删除 dB 文件,2)删除迁移,然后 3)执行 makemigrations your_app_name,然后 4)迁移,然后 5)运行服务器,您应该可以开始了。


小智 10

只需删除除init python文件之外的所有迁移文件,然后运行 ​​python manage.py makemigrations 然后 python manage.py migrate

  • 如果您的数据库中已有数据,请不要执行此操作。 (18认同)
  • 在没有任何上下文的情况下,这对手头的问题毫无帮助,请不要执行上述操作 (8认同)

srv*_*aqa 9

问题出在迁移文件中。我刚刚打开并将默认值从字符串类型更改为整数。

  • 您从哪些迁移文件中进行了更改? (2认同)

Yab*_*esh 7

从迁移文件中,我们可以找到以(001_initial.py,002_auto.py,003_,004_)开头的文件。在这些文件中,将默认值空字符串 (default= '') 或 django.utils.timezone.now() 替换为整数,例如 (default=1)。它可能会解决问题。在此输入图像描述