[英]ValueError: Field 'id' expected a number but got 'Processing'
我将在 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
只需删除除init python 文件之外的所有迁移文件,然后运行 python manage.py makemigrations 然后 python manage.py migrate
只是分享解决我收到的类似错误的解决方案:在我的情况下,收到了同样的错误,因为我直接使用字段值创建模型,而不指定字段名称,因此 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) 运行服务器,您应该一切顺利。
问题出在迁移文件中。 我刚刚打开并将默认值从字符串类型更改为整数。
我在使用不需要强制参数的简单 CBV 时遇到了同样的问题,例如 TemplateView 或 ListView。 我很确定问题来自 url 解释。 对于一个简单的 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'),
在路径工作后添加任何带斜杠(/)的东西?!...
什么都不做有两种方法:1)删除所有迁移并在终端中键入: python3 manage.py makemigrations <app_name>
然后python3 manage.py migrate
然后 runserver。
2)刚刚打开并将默认值从字符串类型更改为整数。
在我看来,选择第一个选项,因为它是安全的方式。
谢谢
请删除您最近创建的迁移文件,然后运行python manage.py makemigrations和python manage.py migrate 。 我认为您的问题会解决。请立即尝试。
我也多次遇到这个问题,我只是简单地删除了我项目中除 init.py 文件之外的所有迁移文件。 然后运行“python manage.py makemigrations”然后运行“python manage.py migrate”就是这样......
转到迁移文件并找到名称 id 是在哪里生成,然后只需将 id 替换为处理器或其他名称或将其删除,因为它定义为verbose_name。
`python manage.py makemigrations`
`python manage.py migrate`
`python manage.py runserver`
正如 utilde 所说,删除迁移目录的内容(除了init .py)以及 _ pycache的内容(除了init .... 文件)。 然后 makemigration 并再次迁移。
不幸的是,我遇到了这个问题,但我被迫从一开始就创建了一个新的 Django 项目,以解决这个问题。 仅仅删除迁移文件并不能解决它。
我遇到过类似的错误。 我原来的网址是这个:
v1/unit/
清理迁移文件对我没有帮助。 幸运的是,它在使用附加字符串扩展 URL 后才开始工作:
v1/unit/addProduct/
我通过删除db
、 migrations
和pycache
文件然后再次运行migrations
和migrate
来解决它。
当我从未将created
添加到get_or_create
时出现此错误
someVariable = "someVariable"
# This will give the error "ValueError: Field 'id' expected a number but got someVariable"
my_model = myModel.objects.get_or_create(someField=someVariable)
添加这个就解决了
# added created
my_model, created = myModel.objects.get_or_create(someField=someVariable)
请参阅此https://docs.djangoproject.com/en/4.0/ref/models/querysets/#get-or-create
我遇到了类似的错误,终于找到了原因。 只是在这里分享,以防有人犯了类似的错误。
我在我的模型上定义了一个自定义的__init__
方法,并且在将 kwargs 传递给super
时错误地只写了一个*
:
def __init__(self, *args, **kwargs):
super().init(*args, *kwargs)
这导致了错误消息:
ValueError: Field 'id' expected a number but got 'account'.
account
是我在视图中创建新实例时传递的第一个关键字参数。 事后看来,错误消息是有道理的,但我花了一个小时的困惑调试才注意到我的错字。
您可以执行以下操作:
Clean up your dbase by deleting entries
or putting in values for missing/wrong entries
清理您的数据库
确保模型中的值是blank=False, null=False
并defaults given
,然后重新运行迁移
设置条件,例如 if Table.field:= "":
或任何导致序列化程序出现问题的值
检查您的所有项目是否都有 id
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.