Python:如何解决问题:'badly formed hexadecimal UUID string' 在 Django 中

Python: How to solve the issue : 'badly formed hexadecimal UUID string' in Django

我创建了 'post' 模型,其中包含 'post_id' 作为主键字段。当我尝试创建 post 时,它引发了一个错误:'badly formed hexadecimal UUID string'。我很乐意帮助我解决这个问题。

这是我的代码。

Models.py:

class Post(models.Model):

    post_id = models.UUIDField(primary_key=True, default='uuid.uuid4', editable=False)
    user = models.ForeignKey(settings.AUTH_USER_MODEL, default=1)
    from1 = models.CharField(max_length=20)
    To = models.CharField(max_length=20)
    timestamp = models.DateTimeField(auto_now=False, auto_now_add=True)

    objects = PostManager()

    def __unicode__(self):
        return self.post_id

    def __str__(self):
        return self.post_id

    def get_absolute_url(self):
        return reverse("posts:detail", kwargs={"post_id": self.post_id})

    class Meta:
        ordering = ["-timestamp", "-Time"]

views.py:

def post_create(request):

    form = PostForm(request.POST or None)
    if form.is_valid():
        instance = form.save(commit=False)
        print(form.cleaned_data.get("post_id"))
        instance.user = request.user
        instance.save()
        return HttpResponseRedirect(instance.get_absolute_url())
    context = {
        "form": form,
    }
    return render(request, "loggedin_load/post_load.html", context)

我会这样做:

from django.utils.encoding import python_2_unicode_compatible

@python_2_unicode_compatible
class Post(models.Model):
    # Instead of default, maybe do null=True to take old entries into account?
    user = models.ForeignKey(settings.AUTH_USER_MODEL, default=1)
    from1 = models.CharField(max_length=20)
    # You may want to reconsider the naming of the "To" field
    # to avoid capital letters and built-in functions
    To = models.CharField(max_length=20)
    timestamp = models.DateTimeField(auto_now_add=True)

    objects = PostManager()

    # You can remove this with the decorator above
    # def __unicode__(self):
        # return self.id

    def __str__(self):
        return self.id  # acts as your post_id

    def get_absolute_url(self):
        return reverse("posts:detail", kwargs={"post_id": self.id})

    class Meta:
        ordering = ["-timestamp", "-Time"]

无论何时创建一个对象,都会自动为其分配一个 ID,该 ID 将填充您的 __unicode____str__get_absolute_url

您需要导入模块并且不要在 'uuid.uuid4' 周围使用引号。

应该有点像:

import uuid  # The uuid module
class Post(models.Model):

    post_id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)  # using the function uuid4 on the module
    user = models.ForeignKey(settings.AUTH_USER_MODEL, default=1)
    from1 = models.CharField(max_length=20)
    To = models.CharField(max_length=20)
    timestamp = models.DateTimeField(auto_now=False, auto_now_add=True)

    objects = PostManager()

    def __unicode__(self):
        return self.post_id

    def __str__(self):
        return self.post_id

    def get_absolute_url(self):
        return reverse("posts:detail", kwargs={"post_id": self.post_id})

    class Meta:
        ordering = ["-timestamp", "-Time"]

N.B 我没有测试上面的代码,我同意一些评论,你不需要 post_id 的 UUID。在不了解更多的情况下,我无能为力。