我正在测试和准备一个新的 Django 包,用于在 Django ORM 和 DRF 中使用带有 Text 和 Char 字段的漂白剂。然而,我遇到了一些障碍,它让我停下来想知道我是否真正理解模型字段是如何实例化的。希望有人可以解决这个问题。
我通过从 django.conf.settings 加载默认设置字典来初始化漂白的参数,然后检查 field_args 参数以查看是否已针对特定字段定义覆盖任何参数,如下所示。然后在 pre_save 函数中使用它来调用漂白:
class BleachedCharField(CharField):
"""
An enhanced CharField for sanitising input with the Python library, bleach.
"""
def __init__(self, *args, field_args=None, **kwargs):
"""
Initialize the BleachedCharField with default arguments, and update with called parameters.
:param tags: (dict) optional bleach argument overrides, format matches BLEACHFIELDS defaults.
:param args: extra args to pass to CharField __init__
:param kwargs: undefined args
"""
super(BleachedCharField, self).__init__(*args, **kwargs)
self.args = settings.BLEACHFIELDS or None
if field_args:
if 'tags' in field_args:
self.args['tags'] = field_args['tags']
if 'attributes' in field_args:
self.args['attributes'] = field_args['attributes']
if 'styles' in field_args:
self.args['styles'] = field_args['styles']
if 'protocols' in field_args:
self.args['protocols'] = field_args['protocols']
if 'strip' in field_args:
self.args['strip'] = field_args['strip']
if 'strip_comments' in field_args:
self.args['strip_comments'] = field_args['strip_comments']
def pre_save(self, model_instance, add):
"""
Clean text, update model and return cleaned text.
:param model_instance: (obj) model instance
:param add: default textfield parameter, unused
:return: clean text as unicode
"""
bleached = clean(getattr(model_instance, self.attname), **self.args)
setattr(model_instance, self.attname, bleached)
return bleached
我遇到的问题是self.args
模型上所有字段的值似乎是模型上加载的最后一个字段的值。例如,在这个模型上:
class Writing(models.Model):
"""
Stores a single writing of a specific Form ( relation :model:`writings.WritingForm` ) and
Category ( relation :model:`writings.Category` ).
"""
author = models.ForeignKey(
settings.AUTH_USER_MODEL,
on_delete=models.CASCADE,
help_text=trans("Author")
)
title = BleachedCharField(
max_length=200,
help_text=trans("Title")
)
created = models.DateTimeField(
auto_now_add=True,
help_text=trans("First created.")
)
edited = models.DateTimeField(
auto_now_add=True,
help_text=trans("Last edited.")
)
description = BleachedTextField(
blank=True,
help_text=trans("A short description of the writing to entice potential readers.")
)
body = BleachedTextField(
field_args=settings.PERMISSIVE_BLEACHFIELDS,
help_text=trans("The body of the writing itself.")
)
writing_form = models.ForeignKey(
WritingForm,
on_delete=models.CASCADE,
help_text=trans("Primary writing form.")
)
category = models.ForeignKey(
Category,
on_delete=models.CASCADE,
help_text=trans("Writing form category")
)
slug = models.SlugField(
editable=False,
help_text=trans("URL and SEO friendly lower-cased string."),
unique=True
)
comments = GenericRelation(settings.COMMENT_MODEL)
在这个模型body
上,作为模型最后一个字段的字段覆盖了它之前的所有 BleachCharField 和 BleachedTextField 实例的 self.args,因此它们都采用相同的参数。
我错过了什么吗?self.args 不是被添加到字段中,而是被添加到模型实例中吗?这就是为什么最后一个字段设置会覆盖所有字段设置的原因吗?我应该怎么做才能避免这个问题?
更新:
为了更加清晰,我添加了 BEACHFIELDS 默认字典和 PERMISSIVE_BLEACHFIELDS 字典:
BLEACHFIELDS = {
'tags': [],
'attributes': {},
'styles': [],
'protocols': [],
'strip': True,
'strip_comments': True
}
PERMISSIVE_BLEACHFIELDS = {
'tags': ['b', 'em', 'i', 'strong', 'span'],
'attributes': {'span': ['style']},
'styles': ['text-decoration', 'font-weight'],
'strip_comments': False
}