def update_mptt_cached_fields(self, instance):
"""
Caches (in an instance._mptt_cached_fields dict) the original values of:
- parent pk
- fields specified in order_insertion_by
These are used in save() to determine if the relevant fields have changed,
so that the MPTT fields need to be updated.
"""
instance._mptt_cached_fields = {}
field_names = set((self.parent_attr,))
if self.order_insertion_by:
for f in self.order_insertion_by:
if f[0] == '-':
f = f[1:]
field_names.add(f)
deferred_fields = instance.get_deferred_fields()
for field_name in field_names:
if deferred_fields:
field = instance._meta.get_field(field_name)
if field.attname in deferred_fields \
and field.attname not in instance.__dict__:
# deferred attribute (i.e. via .only() or .defer())
# It'd be silly to cache this (that'd do a database query)
# Instead, we mark it as a deferred attribute here, then
# assume it hasn't changed during save(), unless it's no
# longer deferred.
instance._mptt_cached_fields[field_name] = DeferredAttribute
continue
instance._mptt_cached_fields[field_name] = self.get_raw_field_value(
instance, field_name)
评论列表
文章目录