Python:如何解决这个问题:Django

时间:2016-07-19 13:25:08

标签: django django-models django-views auto-generate

我创建了'post'模型,其中包含'post_id'作为主键字段。当我试图创建一个帖子时,它会引发一个错误:'形成错误的十六进制UUID字符串'。我很感激帮助我解决这个问题。

这是我的代码。

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)

2 个答案:

答案 0 :(得分:2)

我会做这样的事情:

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

答案 1 :(得分:2)

您需要导入模块,而不是在'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的评论。不知道更多我无法进一步帮助。