Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

731
Visualizações
How to add a default array of values ​to ArrayField?

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[]``

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

The default property on an ArrayField should be a callable. You can read more about that here: https://docs.djangoproject.com/en/3.0/ref/contrib/postgres/fields/.

What you are getting by placing directly there list(dict(constants.NOTIFICATION_SOURCE).keys()) is just a warning so it should still add the defaults to the field. By placing this default directly there it will put in the migrations the following thing and the values will be shared across all field instances:

default=['order_status_changed', 'new_signal']

To get rid of the warning you should create a function that returns the default value:

def get_email_default():
    return list(dict(constants.NOTIFICATION_SOURCE).keys())

and put the function as the default to the field:

email = ArrayField(models.CharField(
    choices= constants.NOTIFICATION_SOURCE,
    max_length=16
), default=get_email_default)

By doing this the warning will be gone and from the function you can have logic for choosing the default value.

After doing this, in the migrations the default value will look like this:

default=my_model.models.get_email_default
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda