Is it possible to add a default value to ArrayField?
I tried to do this for email field, but this did not work:
constants.py:
ORDER_STATUS_CHANGED = 'order_status_changed'
NEW_SIGNAL = 'new_signal'
NOTIFICATION_SOURCE = (
(ORDER_STATUS_CHANGED, 'Order Status Changed'),
(NEW_SIGNAL, 'New Signal'),
)
models.py:
from notifications import constants
from django.contrib.postgres.fields import ArrayField
class NotificationSetting(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE, primary_key=True, related_name='notification_setting')
telegram = ArrayField(models.CharField(
choices= constants.NOTIFICATION_SOURCE,
max_length=30
), default=list)
email = ArrayField(models.CharField(
choices= constants.NOTIFICATION_SOURCE,
max_length=16
), default=list(dict(constants.NOTIFICATION_SOURCE).keys()))
class Meta:
db_table = 'notification_settings'
def __str__(self):
return f'Notification setting for user {self.user}'
And override the save method of the model would be bad practice, I think.
The problem is that in the django admin site I see that the default values did not count when the object was created. (UPD. Maibe i have problem with my custom ChoiseArrayField widged)
And i get this mesagge:
WARNINGS:
notifications.NotificationSetting.email: (postgres.E003) ArrayField default should be a callable instead of an instance so that it's not shared between all field instances.
HINT: Use a callable instead, e.g., uselistinstead of[]``
listinstead of[]The problem is that in the django admin site I see that the default values did not get out