fromdjango.db.models.queryimportCollectedObjectsdefupdate_related_field(obj,value,field):""" Set `field` to `value` for all objects related to `obj`. Based on heavily off the delete object code: http://code.djangoproject.com/browser/django/trunk/django/db/models/query.py#L824 """# Collect all related objects.related_objs=CollectedObjects()obj._collect_sub_objects(related_objs)classes=related_objs.keys()# Bulk update the objects for performanceforclsinclasses:items=related_objs[cls].items()pk_list=[pkforpk,instanceinitems]cls._default_manager.filter(id__in=pk_list).update(**{field:value})