Mysql 无法添加表情符号';s作为评论;操作错误:字符串值不正确:'\\xF0\\x9F\\xA5\\xB0';对于列';内容';第1行“;

Mysql 无法添加表情符号';s作为评论;操作错误:字符串值不正确:'\\xF0\\x9F\\xA5\\xB0';对于列';内容';第1行“;,mysql,python-3.x,django,Mysql,Python 3.x,Django,当我尝试添加表情符号时,我得到了这个错误,甚至查菲尔德也没有得到同样的错误。 models.py 要允许表情符号,您需要将列排序规则设置为utf8mb4\u unicode\u ci,这将允许所有字符,并且在MySQL连接中,您还需要将charset设置为utf8mb4 class Comment(models.Model): post = models.ForeignKey(Post, related_name='comments', on_delete=models.CASCADE) con

当我尝试添加表情符号时,我得到了这个错误,甚至查菲尔德也没有得到同样的错误。 models.py


要允许表情符号,您需要将列排序规则设置为
utf8mb4\u unicode\u ci
,这将允许所有字符,并且在MySQL连接中,您还需要将
charset
设置为
utf8mb4

class Comment(models.Model):
post = models.ForeignKey(Post, related_name='comments', on_delete=models.CASCADE)
content = models.TextField(max_length=500, blank=False)
author = models.ForeignKey(User, on_delete=models.CASCADE)
date_created = models.DateTimeField(default=timezone.now)

def __str__(self):
    return self.content

def get_absolute_url(self):
    return reverse('post-detail', kwargs={'pk': self.post.pk})  # returns a string to the post detail that uses the pk of the comment instance. post. pk to link to the correct detail page ie. /post/

def save(self, *args, **kwargs):
    super(Comment, self).save(*args, **kwargs)
    n = 4
    truncatewords = Truncator(self.content).words(n)
    notify.send(self.author, recipient=self.post.author, verb='commented "' + truncatewords + '" on your post!', action_object=self.post, description='comment', target=self)