首页 > 解决方案 > /admin/posts/post/add/ 处的 Django DataError 超出范围

问题描述

在 Django Admin 中创建新帖子时出现整数超出范围错误。这是具有 Post 模型的 DRF 项目,该模型使用 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, blank=False, null=False)
    url = models.URLField(_("URL"), unique=True,
                          max_length=155, blank=False, null=False)
    country = models.CharField(
        _("Country"), max_length=120, blank=False, null=False)

    post_image = VersatileImageField(
        "Post image",
        blank=False,
        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, null=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:

标签: python-3.xdjangodjango-rest-frameworkdjango-adminuuid

解决方案


您的跟踪显示该问题与 taggit 库有关。我在您的帖子模型上看不到标签字段,但如果您有,请尝试删除它,进行迁移并重试。


推荐阅读