def get_for_model(self, model, model_db=None): model_db = model_db or router.db_for_write(model) content_type = _get_content_type(model, self.db) return self.filter( content_type=content_type, db=model_db, )
def get_for_model(self, model, model_db=None): model_db = model_db or router.db_for_write(model) content_type = _get_content_type(model, self.db) return self.filter( content_type=content_type, db=model_db, )
def get_deleted(self, model, model_db=None): # Try to do a faster JOIN. model_db = model_db or router.db_for_write(model) connection = connections[self.db] if self.db == model_db and connection.vendor in ("sqlite", "postgresql", "oracle"): content_type = _get_content_type(model, self.db) subquery = SubquerySQL( """ SELECT MAX(V.{id}) FROM {version} V LEFT JOIN {model} ON V.{object_id} = CAST({model}.{model_id} as {str}) WHERE V.{db} = %s AND V.{content_type_id} = %s AND {model}.{model_id} IS NULL GROUP BY V.{object_id} """.format( id=connection.ops.quote_name("id"), version=connection.ops.quote_name(Version._meta.db_table), model=connection.ops.quote_name(model._meta.db_table), model_id=connection.ops.quote_name( model._meta.pk.db_column or model._meta.pk.attname), object_id=connection.ops.quote_name("object_id"), str=Version._meta.get_field("object_id").db_type( connection), db=connection.ops.quote_name("db"), content_type_id=connection.ops.quote_name( "content_type_id"), ), (model_db, content_type.id), output_field=Version._meta.pk, ) else: # We have to use a slow subquery. subquery = self.get_for_model(model, model_db=model_db).exclude( object_id__in=list( model._default_manager.using(model_db).values_list( "pk", flat=True).order_by().iterator()), ).values_list("object_id").annotate( latest_pk=models.Max("pk")).order_by().values_list("latest_pk", flat=True) # Perform the subquery. return self.filter(pk__in=subquery, )
def field_dict(self): """ A dictionary mapping field names to field values in this version of the model. This method will follow parent links, if present. """ field_dict = self._local_field_dict # Add parent data. for parent_model, field in self._model._meta.concrete_model._meta.parents.items(): content_type = _get_content_type(parent_model, self._state.db) parent_id = field_dict[field.attname] parent_version = self.revision.version_set.get( content_type=content_type, object_id=parent_id, db=self.db, ) field_dict.update(parent_version.field_dict) return field_dict
def field_dict(self): """ A dictionary mapping field names to field values in this version of the model. This method will follow parent links, if present. """ field_dict = self._local_field_dict # Add parent data. for parent_model, field in self._model._meta.concrete_model._meta.parents.items(): content_type = _get_content_type(parent_model, self._state.db) parent_id = field_dict[field.attname] parent_version = self.revision.version_set.get( content_type=content_type, object_id=parent_id, db=self.db, ) field_dict.update(parent_version.field_dict) return field_dict
def get_deleted(self, model, model_db=None): # Try to do a faster JOIN. model_db = model_db or router.db_for_write(model) connection = connections[self.db] if self.db == model_db and connection.vendor in ("sqlite", "postgresql", "oracle"): content_type = _get_content_type(model, self.db) subquery = SubquerySQL( """ SELECT MAX(V.{id}) FROM {version} V LEFT JOIN {model} ON V.{object_id} = CAST({model}.{model_id} as {str}) WHERE V.{db} = %s AND V.{content_type_id} = %s AND {model}.{model_id} IS NULL GROUP BY V.{object_id} """.format( id=connection.ops.quote_name("id"), version=connection.ops.quote_name(Version._meta.db_table), model=connection.ops.quote_name(model._meta.db_table), model_id=connection.ops.quote_name(model._meta.pk.db_column or model._meta.pk.attname), object_id=connection.ops.quote_name("object_id"), str=Version._meta.get_field("object_id").db_type(connection), db=connection.ops.quote_name("db"), content_type_id=connection.ops.quote_name("content_type_id"), ), (model_db, content_type.id), output_field=Version._meta.pk, ) else: # We have to use a slow subquery. subquery = self.get_for_model(model, model_db=model_db).exclude( object_id__in=list( model._default_manager.using(model_db).values_list("pk", flat=True).order_by().iterator() ), ).values_list("object_id").annotate( latest_pk=models.Max("pk") ).order_by().values_list("latest_pk", flat=True) # Perform the subquery. return self.filter( pk__in=subquery, )