问题描述
在Django Admin中创建新帖子时出现整数超出范围错误。这是DRF项目,具有Post模型,该模型使用UUIDField作为主键作为id。似乎ID字段引起了问题,但我不明白UUIDField怎么可能超出范围。其余所有字段(如“标题”或“ URL”)都不能超出范围。
当我在管理控制台中创建新帖子时,单击“保存”按钮后出现以下错误消息:
DataError at /admin/posts/post/add/
integer out of range
Request Method: POST
Request URL: http://localhost:8000/admin/posts/post/add/
Django Version: 3.0.7
Exception Type: DataError
Exception Value:
integer out of range
Exception Location: /Users/username/Desktop/Workspace/project-api/venv/lib/python3.7/site-packages/django/db/backends/utils.py in _execute,line 86
Python Executable: /Users/username/Desktop/Workspace/project-api/venv/bin/python
Python Version: 3.7.3
这是模型:
class UUIDModel(models.Model):
id = models.UUIDField(primary_key=True,editable=False,default=uuid.uuid4)
class Meta:
abstract = True
class TimeStampedUUIDModel(UUIDModel):
created_at = models.DateTimeField(auto_now_add=True,editable=False)
modified_at = models.DateTimeField(auto_now=True,editable=False)
class Meta:
abstract = True
class Post(TimeStampedUUIDModel):
creator = models.ForeignKey(User,on_delete=models.CASCADE,related_name="posts")
title = models.CharField(
_("Title"),max_length=255,blank=False,null=False)
description = models.TextField(
_("Description"),max_length=1500,null=False)
url = models.URLField(_("URL"),unique=True,max_length=155,null=False)
country = models.CharField(
_("Country"),max_length=120,null=False)
post_image = VersatileImageField(
"Post image",null=False,upload_to=post_image_directory,ppoi_field="post_image_ppoi"
)
post_image_ppoi = PPOIField()
STATUS_DRAFT = "D"
STATUS_PUBLISHED = "P"
STATUSES = (
(STATUS_DRAFT,"Draft"),(STATUS_PUBLISHED,"Published"),)
status = models.CharField(blank=False,choices=STATUSES,default=STATUS_DRAFT,max_length=2)
... ...
我查看了各种答案,但找不到整数超出范围问题的答案。跟踪显示以下消息:
2020-08-18 10:03:44,953:[ERROR]:logger=django.request:request_id=a6d4549d7bfb42f2b780473bcc19ebdf message='Internal Server Error: /admin/posts/post/add/'
Traceback (most recent call last):
File "/Users/username/Desktop/Workspace/project-api/venv/lib/python3.7/site-packages/django/db/models/query.py",line 559,in get_or_create
return self.get(**kwargs),False
File "/Users/username/Desktop/Workspace/project-api/venv/lib/python3.7/site-packages/django/db/models/query.py",line 417,in get
self.model._meta.object_name
taggit.models.TaggedItem.DoesNotExist: TaggedItem matching query does not exist.
During handling of the above exception,another exception occurred:
Traceback (most recent call last):
File "/Users/username/Desktop/Workspace/project-api/venv/lib/python3.7/site-packages/django/db/backends/utils.py",line 86,in _execute
return self.cursor.execute(sql,params)
psycopg2.errors.NumericValueOutOfRange: integer out of range
The above exception was the direct cause of the following exception:
解决方法
您的跟踪显示该问题与taggit库有关。我在您的帖子模型上没有看到标签字段,但是如果有,请尝试将其删除,进行迁移,然后重试。