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

前端 未结 2 1100
北恋
北恋 2021-01-21 18:16

I have created \'post\' model in which I included \'post_id\' as the primary key field. When I am trying to create a post, it is raising an error: \'badly formed hexadecimal UUI

2条回答
  •  野的像风
    2021-01-21 18:22

    I would do something like this:

    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"]
    

    Whenever an object is created, it will automatically be assigned an id, which will populate your __unicode__, __str__, and get_absolute_url.

提交回复
热议问题