def add(self, *new_items): """ Add the passed items to the stored object set, but do not commit them to the database """ items = self.get_object_list() for target in new_items: if target.pk is None: raise ValueError( '"%r" needs to have a primary key value before ' 'it can be added to a parental many-to-many relation.' % target) item_matched = False for i, item in enumerate(items): if item == target: # Replace the matched item with the new one. This ensures that any # modifications to that item's fields take effect within the recordset - # i.e. we can perform a virtual UPDATE to an object in the list # by calling add(updated_object). Which is semantically a bit dubious, # but it does the job... items[i] = target item_matched = True break if not item_matched: items.append(target) # Sort list if rel_model._meta.ordering and len(items) > 1: sort_by_fields(items, rel_model._meta.ordering)
def add(self, *new_items): """ Add the passed items to the stored object set, but do not commit them to the database """ items = self.get_object_list() for target in new_items: if target.pk is None: raise ValueError('"%r" needs to have a primary key value before ' 'it can be added to a parental many-to-many relation.' % target) item_matched = False for i, item in enumerate(items): if item == target: # Replace the matched item with the new one. This ensures that any # modifications to that item's fields take effect within the recordset - # i.e. we can perform a virtual UPDATE to an object in the list # by calling add(updated_object). Which is semantically a bit dubious, # but it does the job... items[i] = target item_matched = True break if not item_matched: items.append(target) # Sort list if rel_model._meta.ordering and len(items) > 1: sort_by_fields(items, rel_model._meta.ordering)
def add(self, *new_items): """ Add the passed items to the stored object set, but do not commit them to the database """ items = self.get_object_list() for target in new_items: item_matched = False for i, item in enumerate(items): if item == target: # Replace the matched item with the new one. This ensures that any # modifications to that item's fields take effect within the recordset - # i.e. we can perform a virtual UPDATE to an object in the list # by calling add(updated_object). Which is semantically a bit dubious, # but it does the job... items[i] = target item_matched = True break if not item_matched: items.append(target) # update the foreign key on the added item to point back to the parent instance setattr(target, related.field.name, self.instance) # Sort list if rel_model._meta.ordering and len(items) > 1: sort_by_fields(items, rel_model._meta.ordering)
def add(self, *new_items): """ Add the passed items to the stored object set, but do not commit them to the database """ items = self.get_object_list() # Rule for checking whether an item in the list matches one of our targets. # We can't do this with a simple 'in' check due to https://code.djangoproject.com/ticket/18864 - # instead, we consider them to match IF: # - they are exactly the same Python object (by reference), or # - they have a non-null primary key that matches items_match = lambda item, target: (item is target) or (item.pk == target.pk and item.pk is not None) for target in new_items: item_matched = False for i, item in enumerate(items): if items_match(item, target): # Replace the matched item with the new one. This ensures that any # modifications to that item's fields take effect within the recordset - # i.e. we can perform a virtual UPDATE to an object in the list # by calling add(updated_object). Which is semantically a bit dubious, # but it does the job... items[i] = target item_matched = True break if not item_matched: items.append(target) # update the foreign key on the added item to point back to the parent instance setattr(target, related.field.name, self.instance) # Sort list if rel_model._meta.ordering and len(items) > 1: sort_by_fields(items, rel_model._meta.ordering)
def set(self, objs, bulk=True, clear=False): # cast objs to a list so that: # 1) we can call len() on it (which we can't do on, say, a queryset) # 2) if we need to sort it, we can do so without mutating the original objs = list(objs) cluster_related_objects = self._get_cluster_related_objects() # Clone and sort the 'objs' list, if necessary if rel_model._meta.ordering and len(objs) > 1: sort_by_fields(objs, rel_model._meta.ordering) cluster_related_objects[relation_name] = objs
def set(self, objs, bulk=True, clear=False): # cast objs to a list so that: # 1) we can call len() on it (which we can't do on, say, a queryset) # 2) if we need to sort it, we can do so without mutating the original objs = list(objs) cluster_related_objects = self._get_cluster_related_objects() for obj in objs: # update the foreign key on the added item to point back to the parent instance setattr(obj, related.field.name, self.instance) # Clone and sort the 'objs' list, if necessary if rel_model._meta.ordering and len(objs) > 1: sort_by_fields(objs, rel_model._meta.ordering) cluster_related_objects[relation_name] = objs
def set(self, objs, bulk=True, clear=False): # cast objs to a list so that: # 1) we can call len() on it (which we can't do on, say, a queryset) # 2) if we need to sort it, we can do so without mutating the original objs = list(objs) if objs and not isinstance(objs[0], rel_model): # assume objs is a list of pks (like when loading data from a # fixture), and allow the orignal manager to handle things original_manager = self.get_original_manager() original_manager.set(objs) return cluster_related_objects = self._get_cluster_related_objects() # Clone and sort the 'objs' list, if necessary if rel_model._meta.ordering and len(objs) > 1: sort_by_fields(objs, rel_model._meta.ordering) cluster_related_objects[relation_name] = objs
def add(self, *new_items): items = self.get_object_list() def items_match(item, target): return (item is target) or (item.pk == target.pk and item.pk is not None) for target in new_items: item_matched = False for i, item in enumerate(items): if items_match(item, target): items[i] = target item_matched = True break if not item_matched: items.append(target) setattr(target, related.field.name, self.instance) if rel_model._meta.ordering and len(items) > 1: sort_by_fields(items, rel_model._meta.ordering)
def add(self, *new_items): """ Add the passed items to the stored object set, but do not commit them to the database """ items = self.get_object_list() # Rule for checking whether an item in the list matches one of our targets. # We can't do this with a simple 'in' check due to https://code.djangoproject.com/ticket/18864 - # instead, we consider them to match IF: # - they are exactly the same Python object (by reference), or # - they have a non-null primary key that matches def items_match(item, target): return (item is target) or (item.pk == target.pk and item.pk is not None) for target in new_items: item_matched = False for i, item in enumerate(items): if items_match(item, target): # Replace the matched item with the new one. This ensures that any # modifications to that item's fields take effect within the recordset - # i.e. we can perform a virtual UPDATE to an object in the list # by calling add(updated_object). Which is semantically a bit dubious, # but it does the job... items[i] = target item_matched = True break if not item_matched: items.append(target) # update the foreign key on the added item to point back to the parent instance setattr(target, related.field.name, self.instance) # Sort list if rel_model._meta.ordering and len(items) > 1: sort_by_fields(items, rel_model._meta.ordering)
def order_by(self, *fields): results = self.results[:] # make a copy of results sort_by_fields(results, fields) return FakeQuerySet(self.model, results)