Django model_to_dict skips all DateTimeField when converting models
auto_now
或auto_now_add
设置为True
,将导致该字段设置为editable=False
和blank=True
。重写(自定义)model_to_dict
class BaseModel(models.Model):
created_at = models.DateTimeField(auto_now_add=True, verbose_name='登録日時')
updated_at = models.DateTimeField(auto_now=True, verbose_name='更新日時')
# 调用时导入
from django.forms.models import model_to_dict
# 定义参照
def model_to_dict(instance, fields=None, exclude=None):
"""
Return a dict containing the data in ``instance`` suitable for passing as
a Form's ``initial`` keyword argument.
``fields`` is an optional list of field names. If provided, return only the
named.
``exclude`` is an optional list of field names. If provided, exclude the
named from the returned dict, even if they are listed in the ``fields``
argument.
"""
opts = instance._meta
data = {}
for f in chain(opts.concrete_fields, opts.private_fields, opts.many_to_many):
if not getattr(f, 'editable', False): # 这里导致updated_at被跳过
continue
if fields is not None and f.name not in fields:
continue
if exclude and f.name in exclude:
continue
data[f.name] = f.value_from_object(instance)
return data
注:若重写该方法,调用时,要import自定义的方法。
# 重写 model_to_dict
# 形参添加 has_editable,默认True
# 调用时:from .util.convert import model_to_dict
>pj>util>convert.py
def model_to_dict(instance, fields=None, exclude=None, has_editable=True):
'''
model to dict。
Args:
instance: model
fields: プロパティを指定する
exclude: プロパティを除外する
has_editable: 自動更新も含めて処理する
return:
dict
'''
opts = instance._meta
data = {}
for f in chain(opts.concrete_fields, opts.private_fields, opts.many_to_many):
if not has_editable and not getattr(f, "editable", False):
# 若不处理自己动更新字段,则跳过
continue
if fields is not None and f.name not in fields:
continue
if exclude and f.name in exclude:
continue
data[f.name] = f.value_from_object(instance)
return data
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。