How to follow the warnings?
models.py
from django.contrib.postgres.fields import JSONField
from django.db import models
from django_extensions.db.models import TimeStampedModel
class UnderwritingValidator(TimeStampedModel):
plan = models.PositiveIntegerField(null=True, blank=True, unique=True)
logic = JSONField(default=dict(
accept_list=[],
reject_list=[]
))
Then makemigrations
WARNINGS:
uw_validators.UnderwritingValidator.logic: (postgres.E003) JSONField 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 `dict` instead of `{}`.
Migrations for 'uw_validators':
uw_validators/migrations/0002_auto_20191011_0321.py
- Remove field accept_list from underwritingvalidator
- Remove field reject_list from underwritingvalidator
- Add field logic to underwritingvalidator
Software:
postgres: 10.9
Django==2.2.5
Questions:
That's not a callable.
You have two options here:
dict
as a default; this will result in your models using an empty dict {}
if none is supplied:class UnderwritingValidator(TimeStampedModel):
plan = models.PositiveIntegerField(null=True, blank=True, unique=True)
logic = JSONField(default=dict)
def get_default_something():
return {'accept_list': [], 'reject_list': []}
class UnderwritingValidator(TimeStampedModel):
plan = models.PositiveIntegerField(null=True, blank=True, unique=True)
logic = JSONField(default=get_default_something)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With