Django Admin Model ArrayField Change Separator

My model looks like this:

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')

This allows me to enter a comma separated list of solutions by default. For example: I can enter in this text box:

1. watch out for dust.,
2. keep away from furry animals.,

This creates a list of two separate string elements. However, if the decision text itself contains a comma, for example:

1. cockroaches, polens and molds might be harmful. 

This will create two separate solution lines due to the presence of a comma in this sentence.

How to tell django to use a different separator than a comma, as this will almost certainly be part of the sentences. How to use delimiter as '|'? I looked into the array class, but it does not allow separation.

0
source share
2 answers

Some related documentation:

ModelForm - , , , SimpleArrayField. , . :

, , . .


, , , ...

# 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
+4

( __init__ Form) .

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 .

0

Source: https://habr.com/ru/post/1694973/


All Articles