是否有将 Wagtail RichTextBlock 内容存储到 UTF-8 数据库的开关?
Is there a switch to store the Wagtail RichTextBlock content to database in UTF-8?
我是 Django 和 Wagtail 的新手。我使用 Django 4.0 和 Wagtail 2.16.1 开发了一个网站应用程序。我发现 Django models.CharField 默认以 UTF-8 格式将内容存储到数据库,而 Wagtail RichTextBlock 默认以 Unicode 格式将内容存储到数据库,这在搜索 East-Asian 个字符时会导致问题。
models.py
class BlogDetailPage(Page):
custom_title = models.CharField('Title', max_length=60, help_text='文章标题')
author = models.ForeignKey(User, on_delete=models.PROTECT)
create_date = models.DateField("Create date", auto_now_add= True)
update_date = models.DateField("Update date", auto_now=True)
intro = models.CharField('Introduction', max_length=500, help_text='文章简介')
tags = ClusterTaggableManager(through=BlogPageTag, blank=True)
content = StreamField(
[
('heading', blocks.CharBlock(form_classname="full title")),
('paragraph', blocks.RichTextBlock()),
('image', ImageChooserBlock()),
('blockquote', blocks.BlockQuoteBlock(label='Block Quote')),
('documentchooser', DocumentChooserBlock(label='Document Chooser')),
('url', blocks.URLBlock(label='URL')),
('embed', EmbedBlock(label='Embed')),
#('snippetchooser', SnippetChooserBlock(label='Snippet Chooser')),
('rawhtml', blocks.RawHTMLBlock(label='Raw HTML')),
('table', TableBlock(label='Table')),
('markdown', MarkdownBlock(label='Markdown')),
('code', CodeBlock(label='Code')),
('imagedeck', CardBlock(label='Imagedeck')),
],
null=True,
blank=True,
)
search.py
def search(request):
search_query = request.GET.get('query', None).strip()
page_num = request.GET.get('page', 1)
condition = None
for word in search_query.split(' '):
if condition is None:
condition = Q(custom_title__icontains=word) | Q(intro__icontains=word) | Q(content__icontains=word.encode('utf-8'))
else:
condition = condition | Q(custom_title__icontains=word) | Q(intro__icontains=word) | Q(content__icontains=word.encode('utf-8'))
search_results = []
if condition is not None:
search_results = BlogDetailPage.objects.live().filter(condition)
问题是我在介绍栏可以搜索英文和中文,但在内容栏只能搜索英文。检查数据库时(默认为 PostgreSQL UTF-8),我发现 Intro 字段是 UTF-8,而 content 字段是 Unicode。想知道是否有将 RichTextField 存储设置为 UTF-8 的设置?
发现几年前有人提出过同样的问题。 Wagtail default search not working with not english fields。
复制 2 个字段仅用于搜索索引的变通方法显然不是一个优雅的解决方案。我使用了第二个答案的想法,即继承 StreamField 并覆盖 get_prep_value.
models.py
class CustomStreamField(StreamField):
def get_prep_value(self, value):
if isinstance(value, blocks.StreamValue) and not(value) and value.raw_text is not None:
# An empty StreamValue with a nonempty raw_text attribute should have that
# raw_text attribute written back to the db. (This is probably only useful
# for reverse migrations that convert StreamField data back into plain text
# fields.)
return value.raw_text
else:
return json.dumps(self.stream_block.get_prep_value(value), ensure_ascii=False, cls=DjangoJSONEncoder)
class BlogDetailPage(Page):
template = "blog/blog_detail_page.html"
custom_title = models.CharField('Title', max_length=60, help_text='文章标题')
author = models.ForeignKey(User,on_delete=models.PROTECT)
create_date = models.DateField("Create date", auto_now_add= True)
update_date = models.DateField("Update date", auto_now=True)
intro = models.CharField('Introduction', max_length=500, help_text='文章简介')
tags = ClusterTaggableManager(through=BlogPageTag, blank=True)
#categories = ParentalManyToManyField('blog.BlogCategory', blank=True)
content = CustomStreamField(
[
('heading', blocks.CharBlock(form_classname="full title")),
('paragraph', blocks.RichTextBlock()),
('image', ImageChooserBlock()),
('blockquote', blocks.BlockQuoteBlock(label='Block Quote')),
('documentchooser', DocumentChooserBlock(label='Document Chooser')),
('url', blocks.URLBlock(label='URL')),
('embed', EmbedBlock(label='Embed')),
#('snippetchooser', SnippetChooserBlock(label='Snippet Chooser')),
('rawhtml', blocks.RawHTMLBlock(label='Raw HTML')),
('table', TableBlock(label='Table')),
('markdown', MarkdownBlock(label='Markdown')),
('code', CodeBlock(label='Code')),
('imagedeck', CardBlock(label='Imagedeck')),
创建的新博客将以 UTF-8 格式存储在数据库中。至于已有的博客,再发一次吧,内容由Unicode改为UTF-8
我是 Django 和 Wagtail 的新手。我使用 Django 4.0 和 Wagtail 2.16.1 开发了一个网站应用程序。我发现 Django models.CharField 默认以 UTF-8 格式将内容存储到数据库,而 Wagtail RichTextBlock 默认以 Unicode 格式将内容存储到数据库,这在搜索 East-Asian 个字符时会导致问题。
models.py
class BlogDetailPage(Page):
custom_title = models.CharField('Title', max_length=60, help_text='文章标题')
author = models.ForeignKey(User, on_delete=models.PROTECT)
create_date = models.DateField("Create date", auto_now_add= True)
update_date = models.DateField("Update date", auto_now=True)
intro = models.CharField('Introduction', max_length=500, help_text='文章简介')
tags = ClusterTaggableManager(through=BlogPageTag, blank=True)
content = StreamField(
[
('heading', blocks.CharBlock(form_classname="full title")),
('paragraph', blocks.RichTextBlock()),
('image', ImageChooserBlock()),
('blockquote', blocks.BlockQuoteBlock(label='Block Quote')),
('documentchooser', DocumentChooserBlock(label='Document Chooser')),
('url', blocks.URLBlock(label='URL')),
('embed', EmbedBlock(label='Embed')),
#('snippetchooser', SnippetChooserBlock(label='Snippet Chooser')),
('rawhtml', blocks.RawHTMLBlock(label='Raw HTML')),
('table', TableBlock(label='Table')),
('markdown', MarkdownBlock(label='Markdown')),
('code', CodeBlock(label='Code')),
('imagedeck', CardBlock(label='Imagedeck')),
],
null=True,
blank=True,
)
search.py
def search(request):
search_query = request.GET.get('query', None).strip()
page_num = request.GET.get('page', 1)
condition = None
for word in search_query.split(' '):
if condition is None:
condition = Q(custom_title__icontains=word) | Q(intro__icontains=word) | Q(content__icontains=word.encode('utf-8'))
else:
condition = condition | Q(custom_title__icontains=word) | Q(intro__icontains=word) | Q(content__icontains=word.encode('utf-8'))
search_results = []
if condition is not None:
search_results = BlogDetailPage.objects.live().filter(condition)
问题是我在介绍栏可以搜索英文和中文,但在内容栏只能搜索英文。检查数据库时(默认为 PostgreSQL UTF-8),我发现 Intro 字段是 UTF-8,而 content 字段是 Unicode。想知道是否有将 RichTextField 存储设置为 UTF-8 的设置?
发现几年前有人提出过同样的问题。 Wagtail default search not working with not english fields。 复制 2 个字段仅用于搜索索引的变通方法显然不是一个优雅的解决方案。我使用了第二个答案的想法,即继承 StreamField 并覆盖 get_prep_value.
models.py
class CustomStreamField(StreamField):
def get_prep_value(self, value):
if isinstance(value, blocks.StreamValue) and not(value) and value.raw_text is not None:
# An empty StreamValue with a nonempty raw_text attribute should have that
# raw_text attribute written back to the db. (This is probably only useful
# for reverse migrations that convert StreamField data back into plain text
# fields.)
return value.raw_text
else:
return json.dumps(self.stream_block.get_prep_value(value), ensure_ascii=False, cls=DjangoJSONEncoder)
class BlogDetailPage(Page):
template = "blog/blog_detail_page.html"
custom_title = models.CharField('Title', max_length=60, help_text='文章标题')
author = models.ForeignKey(User,on_delete=models.PROTECT)
create_date = models.DateField("Create date", auto_now_add= True)
update_date = models.DateField("Update date", auto_now=True)
intro = models.CharField('Introduction', max_length=500, help_text='文章简介')
tags = ClusterTaggableManager(through=BlogPageTag, blank=True)
#categories = ParentalManyToManyField('blog.BlogCategory', blank=True)
content = CustomStreamField(
[
('heading', blocks.CharBlock(form_classname="full title")),
('paragraph', blocks.RichTextBlock()),
('image', ImageChooserBlock()),
('blockquote', blocks.BlockQuoteBlock(label='Block Quote')),
('documentchooser', DocumentChooserBlock(label='Document Chooser')),
('url', blocks.URLBlock(label='URL')),
('embed', EmbedBlock(label='Embed')),
#('snippetchooser', SnippetChooserBlock(label='Snippet Chooser')),
('rawhtml', blocks.RawHTMLBlock(label='Raw HTML')),
('table', TableBlock(label='Table')),
('markdown', MarkdownBlock(label='Markdown')),
('code', CodeBlock(label='Code')),
('imagedeck', CardBlock(label='Imagedeck')),
创建的新博客将以 UTF-8 格式存储在数据库中。至于已有的博客,再发一次吧,内容由Unicode改为UTF-8