1 回答
TA贡献1780条经验 获得超5个赞
正如你可能在Django文档中看到的,你可以使用一个值或一个可调用作为默认值。如果您使用可调用的(例如函数),则每次需要默认值时都会调用它。
问题:您正在传递值,因为您正在调用函数 。该函数被调用一次,当您的模块(models.py)导入到应用程序中时。default=return_timestamped_id()
解决方案:传递函数本身default=return_timestamped_id
你可以在django.models.Fields类中看到相关的代码(注释我的):
class Field():
def __init__(self, ..., default=NOT_PROVIDED,...):
...
self.default = default # save the default as a member variable
...
def get_default(self):
"""Return the default value for this field."""
return self._get_default()
@cached_property
def _get_default(self):
if self.has_default():
if callable(self.default): # if it is callable, return it
return self.default
return lambda: self.default # else wrap in a callable
添加回答
举报
