尽管尝试使用 taggit,UUID 作为主键仍会引发溢出错误

发布于 2025-01-13 11:40:53 字数 1676 浏览 2 评论 0原文

我尝试使用 UUID 作为模型上的主键,但在使用重定向时会引发溢出错误。

我在网上查找了类似的问题,因为我认为人们应该经常这样做。我遇到了: https://github.com/jazzband/django-taggit/issues/679

所以我安装了 dajngo-taggit 并尝试在我的代码中添加此链接上的代码片段,但问题仍然存在。

下面是我当前使用代码片段的代码:

[models.py]

from django.db import models
from taggit.managers import TaggableManager
from taggit.models import GenericUUIDTaggedItemBase, TaggedItemBase

class Person(models.Model):
    uuid = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    first_name = models.CharField(max_length=50)
    last_name = models.CharField(max_length=50)
    # ...

class UUIDTaggedItem(GenericUUIDTaggedItemBase, TaggedItemBase):
    # If you only inherit GenericUUIDTaggedItemBase, you need to define
    # a tag field. e.g.
    # tag = models.ForeignKey(Tag, related_name="uuid_tagged_items", on_delete=models.CASCADE)

    class Meta:
        verbose_name = _("Tag")
        verbose_name_plural = _("Tags")

class Car(models.Model):
    person = models.ForeignKey(Person, null=True, on_delete=models.SET_NULL)
    brand = models.CharField(max_length=50)
    colour = models.CharField(max_length=50)
    # ...

[urls.py]

# ...    
urlpatterns = [
    # ...
    path("<uuid:pk>/car/<int:id>/", CarDetail.as_view(), name="car_detail"),
    # ...
]

我得到的错误是:

溢出错误 /en/person/c8c91051-5773-4c69-9587-92b468186db7/car/1/

Python int 太大,无法转换为 SQLite INTEGER

您对我可以尝试修复它有什么想法吗?我已经研究这个问题有一段时间了。谢谢你!!

I am trying to use UUID as a primary key on a model and it throws an Overflow error when using redirect.

I looked online for similar problems as I assume it should be quite frequent that people want to do that. I came accross:
https://github.com/jazzband/django-taggit/issues/679

So I installed dajngo-taggit and I tried to add the snippet on this link in my code but the problem remains.

Below is my current code using the snippet:

[models.py]

from django.db import models
from taggit.managers import TaggableManager
from taggit.models import GenericUUIDTaggedItemBase, TaggedItemBase

class Person(models.Model):
    uuid = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    first_name = models.CharField(max_length=50)
    last_name = models.CharField(max_length=50)
    # ...

class UUIDTaggedItem(GenericUUIDTaggedItemBase, TaggedItemBase):
    # If you only inherit GenericUUIDTaggedItemBase, you need to define
    # a tag field. e.g.
    # tag = models.ForeignKey(Tag, related_name="uuid_tagged_items", on_delete=models.CASCADE)

    class Meta:
        verbose_name = _("Tag")
        verbose_name_plural = _("Tags")

class Car(models.Model):
    person = models.ForeignKey(Person, null=True, on_delete=models.SET_NULL)
    brand = models.CharField(max_length=50)
    colour = models.CharField(max_length=50)
    # ...

[urls.py]

# ...    
urlpatterns = [
    # ...
    path("<uuid:pk>/car/<int:id>/", CarDetail.as_view(), name="car_detail"),
    # ...
]

And the error I get is:

OverflowError at
/en/person/c8c91051-5773-4c69-9587-92b468186db7/car/1/

Python int too large to convert to SQLite INTEGER

Would you have any ideas of what I could try to fix it? I have been on this issue for quite some time. Thank you!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文