Django Admin 模型 ArrayField 更改分隔符
Django Admin Model ArrayField Change Delimiter
我的模型是这样的:
from django.contrib.postgres.fields import ArrayField
class Trigger(models.Model):
solutions = ArrayField(models.TextField(blank=True, null=True), blank=True, null=True, help_text='some helpful text')
默认情况下,这允许我输入以逗号分隔的解决方案列表。例如:我可以在该文本字段中输入:
1. watch out for dust.,
2. keep away from furry animals.,
这确实创建了一个包含两个单独字符串项的列表。但是,如果解决方案文本本身包含逗号,例如:
1. cockroaches, polens and molds might be harmful.
这将创建两个单独的解决方案行,因为该句子中存在逗号。
我如何告诉 django 使用与逗号不同的分隔符,因为它几乎肯定是句子的一部分。我怎样才能使用像'|'这样的分隔符?我查看了数组字段 class,但它不允许任何分隔符。
一些相关文档:
- https://docs.djangoproject.com/en/1.11/ref/contrib/postgres/fields/#arrayfield
- https://docs.djangoproject.com/en/1.11/ref/contrib/postgres/forms/#simplearrayfield
如果您在管理站点上使用内置表单,或者在未自定义任何字段的情况下使用 ModelForm,则该字段可能会自动使用 SimpleArrayField
表单字段。看起来您可以覆盖定界符。文档说明了这个警告:
The field does not support escaping of the delimiter, so be careful in cases where the delimiter is a valid character in the underlying field. The delimiter does not need to be only one character.
无论如何,您可以通过提供这样的自定义表单来做到这一点...
# forms.py
from django import forms
from .models import Trigger
class TriggerForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields['solutions'].delimiter = '|' # Or whichever other character you want.
class Meta:
model = Trigger
fields = '__all__'
如果这是用于管理站点...
# admin.py
from django.contrib import admin
from .forms import TriggerForm
from .models import Trigger
@admin.register(Trigger)
class TriggerAdmin(admin.ModelAdmin):
form = TriggerForm
更简洁的解决方案(不覆盖表单 class 中的 __init__
)是仅在表单上指定字段。
from django.contrib.postgres.forms import SimpleArrayField
from django import forms
from django.forms.fields import CharField
from django.forms.widgets import Textarea
class TriggerForm(forms.ModelForm):
solutions = SimpleArrayField(CharField(), delimiter='|', widget=Textarea())
通过这种方式,您可以轻松地选择使用 SplitArrayField
或为该字段指定不同的小部件。
我的模型是这样的:
from django.contrib.postgres.fields import ArrayField
class Trigger(models.Model):
solutions = ArrayField(models.TextField(blank=True, null=True), blank=True, null=True, help_text='some helpful text')
默认情况下,这允许我输入以逗号分隔的解决方案列表。例如:我可以在该文本字段中输入:
1. watch out for dust.,
2. keep away from furry animals.,
这确实创建了一个包含两个单独字符串项的列表。但是,如果解决方案文本本身包含逗号,例如:
1. cockroaches, polens and molds might be harmful.
这将创建两个单独的解决方案行,因为该句子中存在逗号。
我如何告诉 django 使用与逗号不同的分隔符,因为它几乎肯定是句子的一部分。我怎样才能使用像'|'这样的分隔符?我查看了数组字段 class,但它不允许任何分隔符。
一些相关文档:
- https://docs.djangoproject.com/en/1.11/ref/contrib/postgres/fields/#arrayfield
- https://docs.djangoproject.com/en/1.11/ref/contrib/postgres/forms/#simplearrayfield
如果您在管理站点上使用内置表单,或者在未自定义任何字段的情况下使用 ModelForm,则该字段可能会自动使用 SimpleArrayField
表单字段。看起来您可以覆盖定界符。文档说明了这个警告:
The field does not support escaping of the delimiter, so be careful in cases where the delimiter is a valid character in the underlying field. The delimiter does not need to be only one character.
无论如何,您可以通过提供这样的自定义表单来做到这一点...
# forms.py
from django import forms
from .models import Trigger
class TriggerForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields['solutions'].delimiter = '|' # Or whichever other character you want.
class Meta:
model = Trigger
fields = '__all__'
如果这是用于管理站点...
# admin.py
from django.contrib import admin
from .forms import TriggerForm
from .models import Trigger
@admin.register(Trigger)
class TriggerAdmin(admin.ModelAdmin):
form = TriggerForm
更简洁的解决方案(不覆盖表单 class 中的 __init__
)是仅在表单上指定字段。
from django.contrib.postgres.forms import SimpleArrayField
from django import forms
from django.forms.fields import CharField
from django.forms.widgets import Textarea
class TriggerForm(forms.ModelForm):
solutions = SimpleArrayField(CharField(), delimiter='|', widget=Textarea())
通过这种方式,您可以轻松地选择使用 SplitArrayField
或为该字段指定不同的小部件。