Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django ArrayField Default Should be a Callable

I'm trying to use postgresql ArrayField in my django project setting it a default, but I can't get rid of the warning 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. use list instead of []

This is my code:

def get_default_subscriptions():
    return 'Noticias, Promociones, Pagos, Compras'.split(', ') # this returns a list
 
class myModel(models.model):
    # other fields
    subscriptions = ArrayField(models.CharField(max_length=200), default=get_default_subscriptions()) # this is a callable, so, why the warning?

Docs say about this: If you give the field a default, ensure it’s a callable such as list (for an empty default) or a callable that returns a list (such as a function). Incorrectly using default=[] creates a mutable default that is shared between all instances of ArrayField.

Can you give me an idea of what am I doing wrong?

like image 586
William Colmenares Avatar asked Mar 05 '26 03:03

William Colmenares


1 Answers

Use default=get_default_subscriptions (without paranthesis) instead of default=get_default_subscriptions()

like image 61
JPG Avatar answered Mar 07 '26 16:03

JPG



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!