def iterator(self): """ An iterator over the results from applying this QuerySet to the remote web service. """ try: sql, params = SQLCompiler(self.query, connections[self.db], None).as_sql() except EmptyResultSet: raise StopIteration cursor = CursorWrapper(connections[self.db], self.query) cursor.execute(sql, params) pfd = prep_for_deserialize only_load = self.query.get_loaded_field_names() load_fields = [] # If only/defer clauses have been specified, # build the list of fields that are to be loaded. if not only_load: model_cls = self.model init_list = None else: if DJANGO_16_PLUS: fields = self.model._meta.concrete_fields fields_with_model = self.model._meta.get_concrete_fields_with_model() else: fields = self.model._meta.fields fields_with_model = self.model._meta.get_fields_with_model() for field, model in fields_with_model: if model is None: model = self.model try: selected_name = field.attname if DJANGO_18_PLUS else field.name if selected_name in only_load[model]: # Add a field that has been explicitly included load_fields.append(field.name) except KeyError: # Model wasn't explicitly listed in the only_load table # Therefore, we need to load all fields from this model load_fields.append(field.name) init_list = [] skip = set() for field in fields: if field.name not in load_fields: skip.add(field.attname) else: init_list.append(field.name) model_cls = deferred_class_factory(self.model, skip) field_names = self.query.get_loaded_field_names() for res in python.Deserializer(pfd(model_cls, r, self.db, init_list) for r in cursor.results): # Store the source database of the object res.object._state.db = self.db # This object came from the database; it's not being added. res.object._state.adding = False yield res.object
def Deserializer(stream_or_string, **options): """Deserialize a string or stream of JSON data with files embedded.""" if isinstance(stream_or_string, basestring): stream = StringIO(stream_or_string) else: stream = stream_or_string data = simplejson.load(stream) for name, file_data in data['files'].items(): _write_file(name, file_data) for obj in python_s.Deserializer(data['objects']): yield obj
def iterator(self): """ An iterator over the results from applying this QuerySet to the remote web service. """ sql, params = compiler.SQLCompiler(self.query, connections[self.db], None).as_sql() cursor = CursorWrapper(connections[self.db], self.query) cursor.execute(sql, params) pfd = prep_for_deserialize for res in python.Deserializer( pfd(self.model, r, self.db) for r in cursor.results): yield res.object
def Deserializer(*args, **kwargs): python.Deserializer(*args, **kwargs)
def __iter__(self): queryset = self.queryset """ An iterator over the results from applying this QuerySet to the remote web service. """ try: sql, params = SQLCompiler(queryset.query, connections[queryset.db], None).as_sql() except EmptyResultSet: raise StopIteration cursor = CursorWrapper(connections[queryset.db], queryset.query) cursor.execute(sql, params) only_load = queryset.query.get_loaded_field_names() load_fields = [] # If only/defer clauses have been specified, # build the list of fields that are to be loaded. if not only_load: model_cls = queryset.model init_list = None else: fields = queryset.model._meta.concrete_fields for field in fields: model = field.model._meta.concrete_model if model is None: model = queryset.model try: if field.attname in only_load[model]: # Add a field that has been explicitly included load_fields.append(field.name) except KeyError: # Model wasn't explicitly listed in the only_load table # Therefore, we need to load all fields from this model load_fields.append(field.name) init_list = [] skip = set() for field in fields: if field.name not in load_fields: skip.add(field.attname) else: init_list.append(field.name) if DJANGO_110_PLUS: model_cls = queryset.model else: model_cls = deferred_class_factory(queryset.model, skip) field_names = queryset.query.get_loaded_field_names() for res in python.Deserializer(x for x in ( prep_for_deserialize(model_cls, r, queryset.db, init_list) for r in cursor.results) if x is not None): # Store the source database of the object res.object._state.db = queryset.db # This object came from the database; it's not being added. res.object._state.adding = False if DJANGO_110_PLUS and init_list is not None and len( init_list) != len(model_cls._meta.concrete_fields): raise NotImplementedError( "methods defer() and only() are not implemented for Django 1.10 yet" ) yield res.object