

def prep_localized_datetime(sender, **kwargs):
    for field in sender._meta.fields:
        if ((not isinstance(field, LocalizedDateTimeField)) or (field.timezone is None)):
            continue
        dt_field_name = ('_datetimezone_%s' % field.attname)

        def get_dtz_field(instance):
            return getattr(instance, dt_field_name)

        def set_dtz_field(instance, dt):
            if (dt.tzinfo is None):
                dt = default_tz.localize(dt)
            time_zone = field.timezone
            if isinstance(field.timezone, basestring):
                tz_name = instance._default_manager.filter(pk=model_instance._get_pk_val()).values_list(field.timezone)[0][0]
                try:
                    time_zone = pytz.timezone(tz_name)
                except:
                    time_zone = default_tz
                if (time_zone is None):
                    time_zone = default_tz
            elif callable(time_zone):
                tz_name = time_zone()
                if isinstance(tz_name, basestring):
                    try:
                        time_zone = pytz.timezone(tz_name)
                    except:
                        time_zone = default_tz
                else:
                    time_zone = tz_name
                if (time_zone is None):
                    time_zone = default_tz
            setattr(instance, dt_field_name, dt.astimezone(time_zone))
        setattr(sender, field.attname, property(get_dtz_field, set_dtz_field))

dataset/ETHPy150Open davemerwin/blue-channel/external_apps/timezones/fields.py prep_localized_datetime/original