ValueError:字段“ id”应为数字,但已得到“正在处理”

时间:2019-12-10 11:54:08

标签: 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'.

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

11 个答案:

答案 0 :(得分:4)

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

答案 1 :(得分:0)

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

答案 2 :(得分:0)

我在使用简单的CBV时遇到了同样的问题,例如TemplateView或ListView,它们不需要强制参数。我很确定问题出自网址解释。对于像这样的简单ListView

class ProfileList(generic.ListView):
    model = get_user_model()

网址

path('profile_list/dummy', ProfileList.as_view(), name='profile_lv'),

有效,而下面的无效,错误:字段'id'需要一个数字,但抛出了'profile_lv'。其中 profile_lv 是网址的名称...

path('profile_list', ProfileList.as_view(), name='profile_lv'),

在路径可行之后添加带有斜杠(/)的任何内容吗?!...

答案 3 :(得分:0)

什么也不做有两种方法: 1)删除所有迁移,然后在终端中输入:python3 manage.py makemigrations <app_name>,然后输入python3 manage.py migrate,然后输入runserver。

2)只需打开并将默认值从字符串类型更改为整数即可。

我认为选择第一个选项是因为它是安全的方法。

谢谢

答案 4 :(得分:0)

仅分享与我收到的类似错误一起使用的解决方案: 在我的情况下,由于我直接使用字段值创建模型即时事件,而未指定字段名称,因此收到了相同的错误,因此ID始终将第一个作为默认值(ID = field1)。 通过将属性名称也添加到Instant创建中,解决了该问题。

原是:

model_instant = YourModel(field1, field2,...etc)

解决者:

model_instant = YourModel(field1 = field1, field2 = field2,...etc)

执行此操作,然后按照上面的建议1)删除dB文件,2)删除迁移,然后3)进行makemigrations your_app_name,然后4)迁移,然后5)运行服务器,您应该一切顺利

答案 5 :(得分:0)

请删除您最近创建的迁移文件,然后运行 python manage.py makemigrations python manage.py migration 。我认为您的问题即将解决。请立即尝试。

答案 6 :(得分:0)

我也多次遇到这个问题,我只是简单地删除了我项目中除 init.py 文件之外的所有迁移文件。然后运行“python manage.py makemigrations”,然后运行“python manage.py migrate”就可以了...

答案 7 :(得分:0)

转到迁移文件并找到名称 id 生成的位置,然后只需将 id 替换为处理器或其他名称或将其删除,因为它定义为 verbose_name。

`python manage.py makemigrations`
`python manage.py migrate`
`python manage.py runserver`

答案 8 :(得分:0)

如utilde所说,删除migrations目录的内容(init.py除外)以及_pycache的内容(init< /strong>.... 文件)。 然后进行迁移并再次迁移。

答案 9 :(得分:0)

不幸的是,我遇到了这个问题,但我被迫从一开始就创建了一个新的 Django 项目,以解决这个问题。仅仅删除迁移文件并不能解决问题。

答案 10 :(得分:-1)

检查您所有的物品是否都有ID