简体   繁体   English

如何遵循提示:改用可调用对象,例如,使用 `dict` 代替 `{}`?

[英]How to follow HINT: Use a callable instead, e.g., use `dict` instead of `{}`?

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然后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 postgres:10.9
Django==2.2.5 Django==2.2.5

Questions:问题:

  1. Am I safe from error?我可以避免错误吗? If it safe I will ignore this warning and erase my short warning note如果安全,我将忽略此警告并删除我的简短警告说明
  2. How to fully follow the warning?如何完全遵守警告?

That's not a callable.那不是可调用的。

You have two options here:您在这里有两个选择:

  1. Rely on dict as a default;依赖dict作为默认值; this will result in your models using an empty dict {} if none is supplied:如果没有提供,这将导致您的模型使用空的 dict {}
class UnderwritingValidator(TimeStampedModel):
    plan = models.PositiveIntegerField(null=True, blank=True, unique=True)
    logic = JSONField(default=dict)
  1. Create your own "callable" and use it as default:创建您自己的“可调用”并将其用作默认值:
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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 dict 代替 class - Use of dict instead of class 如何为 wxPython(例如 wxMSW)使用特定于平台的“端口”? - How to use platform specific "ports" for wxPython (e.g. wxMSW)? 如何在Windows cmd上将不支持的unicode字符打印为“?”而不是引发异常? - How to print unsupported unicode characters on Windows cmd as e.g. “?” instead of raising exception? 如何在Python中使用2个小数点显示浮点数(例如,显示8.60而不是8.6) - How to make a float show up with 2 decimal points in Python (e.g. show 8.60 instead of 8.6) 何时使用类而不是字典 - When to use a class instead of a dict 如何使用数字作为变量名,或者使用字典呢? - How to use numbers as variable name, or use a dict instead? 如何使用 SimpleNamespace 的属性列表而不是 dict 键列表? - How to use a list of attributes of SimpleNamespace instead of list of dict keys? 如何强制 Python 给出解决方案而不是 'Nan',例如 scipy.special 导入 kn 中的大量输入 - How to force Python give solutions instead of 'Nan', e.g. large input in scipy.special import kn 使用类实例名称作为图例条目,例如绘图 - Use class instance names as legend entry for e.g. a plot 返回具有动态变量名称的 function 以用于例如 lmfit - Returning a function with dynamic variable names for use in e.g. lmfit
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM