update DB objects related to a base object
            obj:            a base object to create related
            related:        dict mapping field names to lists of related objects
            subfield_list:  where to get the next layer of subfields
543053
def _update_related(self, obj, related, subfield_dict):
        
        # keep track of whether or not anything was updated
        updated = False

        # for each related field - check if there are differences
        for field, items in related.items():
            # skip subitem check if it's locked anyway
            if field in obj.locked_fields:
                continue

            # get items from database
            dbitems = list(getattr(obj, field).all())
            dbitems_count = len(dbitems)

            # default to doing nothing
            do_delete = do_update = False

            if items and dbitems_count:         # we have items, so does db, check for conflict
                do_delete = do_update = items_differ(items, dbitems, subfield_dict[field][2])
            elif items and not dbitems_count:   # we have items, db doesn't, just update
                do_update = True
            elif not items and dbitems_count:   # db has items, we don't, just delete
                do_delete = True
            # otherwise: no items or dbitems, so nothing is done

            # don't delete if field is in merge_related
            if field in self.merge_related:
                new_items = []
                # build a list of keyfields to existing database objects
                keylist = self.merge_related[field]
                keyed_dbitems = {tuple(getattr(item, k) for k in keylist):
                                 item for item in dbitems}

                # go through 'new' items
                #   if item with the same keyfields exists:
                #       update the database item w/ the new item's properties
                #   else:
                #       add it to new_items
                for item in items:
                    key = tuple(item.get(k) for k in keylist)
                    dbitem = keyed_dbitems.get(key)
                    if not dbitem:
                        new_items.append(item)
                    else:
                        # update dbitem
                        for fname, val in item.items():
                            setattr(dbitem, fname, val)
                        dbitem.save()

                # import anything that made it to new_items in the usual fashion
                self._create_related(obj, {field: new_items}, subfield_dict)
            else:
                # default logic is to just wipe and recreate subobjects
                if do_delete:
                    updated = True
                    getattr(obj, field).all().delete()
                if do_update:
                    updated = True
                    self._create_related(obj, {field: items}, subfield_dict)

        return updated
209724
def handle_fk_field(self, obj, field):
        
        self._start_relational_field(field)
        related = getattr(obj, field.name)
        if related is not None:
            if self.use_natural_keys and hasattr(related, 'natural_key'):
                # If related object has a natural key, use it
                related = related.natural_key()
                # Iterable natural keys are rolled out as subelements
                for key_value in related:
                    self.xml.startElement("natural", {})
                    self.xml.characters(smart_unicode(key_value))
                    self.xml.endElement("natural")
            else:
                if field.rel.field_name == related._meta.pk.name:
                    # Related to remote object via primary key
                    related = related._get_pk_val()
                else:
                    # Related to remote object via other field
                    related = getattr(related, field.rel.field_name)
                self.xml.characters(smart_unicode(related))
        else:
            self.xml.addQuickElement("None")
        self.xml.endElement("field")
724590
def change(obj, **changed_fields):
    
    obj_field_names = {
        field.name for field in obj._meta.fields
    } | {
        field.attname for field in obj._meta.fields
    } | {'pk'}

    for field_name, value in changed_fields.items():
        if field_name not in obj_field_names:
            raise ValueError("'{}' is an invalid field name".format(field_name))
        setattr(obj, field_name, value)
    return obj
328180
def _build_dependent_model_list(self, obj_schema):
        '''
        
        '''
        dep_models_list = []

        if obj_schema:
            obj_schema['type'] = obj_schema.get('type', 'object')
        if obj_schema['type'] == 'array':
            dep_models_list.extend(self._build_dependent_model_list(obj_schema.get('items', {})))
        else:
            ref = obj_schema.get('$ref')
            if ref:
                ref_obj_model = ref.split("/")[-1]
                ref_obj_schema = self._models().get(ref_obj_model)
                dep_models_list.extend(self._build_dependent_model_list(ref_obj_schema))
                dep_models_list.extend([ref_obj_model])
            else:
                # need to walk each property object
                properties = obj_schema.get('properties')
                if properties:
                    for _, prop_obj_schema in six.iteritems(properties):
                        dep_models_list.extend(self._build_dependent_model_list(prop_obj_schema))
        return list(set(dep_models_list))
595181
def _put_into_indexes(self, obj):
        
        no_of_used_indexes = 0
        for field_name, db_index in list(self._get_db_fields(obj)):
            attr_value = getattr(obj, field_name)

            if attr_value is None:  # index only by set attributes
                continue

            container = db_index.get(attr_value, None)
            if container is None:
                container = OOTreeSet()
                db_index[attr_value] = container

            container.insert(obj)

            no_of_used_indexes += 1

        # make sure that atleast one `attr_value` was used
        if no_of_used_indexes <= 0:
            raise UnindexableObject(
                "You have to use atleast one of the identificators!"
            )
1374764
def get_related_fields(model_class, field_name, path=""):
    
    if field_name:
        field, model, direct, m2m = _get_field_by_name(model_class, field_name)
        if direct:
            # Direct field
            try:
                new_model = _get_remote_field(field).parent_model()
            except AttributeError:
                new_model = _get_remote_field(field).model
        else:
            # Indirect related field
            if hasattr(field, 'related_model'):  # Django>=1.8
                new_model = field.related_model
            else:
                new_model = field.model()

        path += field_name
        path += '__'
    else:
        new_model = model_class

    new_fields = get_relation_fields_from_model(new_model)
    model_ct = ContentType.objects.get_for_model(new_model)

    return (new_fields, model_ct, path)
519021
def _nary_dict_update(dicts, **kwargs):
    
    copy = kwargs['copy']
    res = dicts[0].copy() if copy else dicts[0]
    if len(dicts) == 1:
        return res

    # decide what strategy to use when updating a dictionary
    # with the values from another: {(non)recursive} x {(non)overwriting}
    deep = kwargs['deep']
    overwrite = kwargs['overwrite']
    if deep:
        dict_update = curry(_recursive_dict_update, overwrite=overwrite)
    else:
        if overwrite:
            dict_update = res.__class__.update
        else:
            def dict_update(dict_, other):
                for k, v in iteritems(other):
                    dict_.setdefault(k, v)

    for d in dicts[1:]:
        dict_update(res, d)
    return res
1109659
def update(self, *objs):
        
        keys = self.keys()
        new_keys = get_objs_columns(objs, self.realfieldb)

        modified = False
        for v in new_keys:
            if v in keys:    #the id has been existed, so don't insert new record
                keys.remove(v)
            else:
                d = {self.fielda:self.valuea, self.fieldb:v}
                if self.before_save:
                    self.before_save(d)
                if self.through_model:
                    obj = self.through_model(**d)
                    obj.save()
                else:
                    self.do_(self.table.insert().values(**d))
                modified = True
                
        if keys: #if there are still keys, so delete them
            self.clear(*keys)
            modified = True
        
        #cache [] to _STORED_attr_name
        setattr(self.instance, self.store_key, new_keys)
        
        return modified
832983
def _m2m_field_data(field, obj):
    
    data = _basic_field_data(field, obj)
    data[Field.TYPE] = FieldType.M2M
    related = field.rel.to
    relation_info = {
        Field.REL_DB_TABLE: related._meta.db_table,
        Field.REL_APP: related._meta.app_label,
        Field.REL_MODEL: related.__name__
    }
    data[Field.REL] = relation_info
    value = data[Field.VALUE]
    value = [x[0] for x in value.values_list('pk')]
    data[Field.VALUE] = value
    return data
495598
def create_order_objects(model, order_fields):
    
    for rel in model._meta.get_all_related_objects():
        rel_model = rel.model
        if rel_model.__module__ == 'order.models':

            objs = model.objects.all()
            values = {}
            for order_field in order_fields:
                order_objs = rel_model.objects.all().order_by('-%s' \
                        % order_field)
                try:
                    values[order_field] = getattr(order_objs[0], \
                            order_field) + 1
                except IndexError:
                    values[order_field] = 1
            for obj in objs:
                try:
                    rel_model.objects.get(item=obj)
                except rel_model.DoesNotExist:
                    rel_model.objects.create(item=obj, **values)
                    for key in values:
                        values[key] += 1
823629
def p_obj_fields(self, p):
        
        p[0] = dict([p[1]] if len(p) == 2 else p[1] + [p[3]])
394771
async def add(self, *instances, using_db=None) -> None:
        
        if not instances:
            return
        if self.instance.id is None:
            raise OperationalError(
                "You should first call .save() on {model}".format(model=self.instance)
            )
        db = using_db if using_db else self.model._meta.db
        through_table = Table(self.field.through)
        select_query = (
            db.query_class.from_(through_table)
            .where(getattr(through_table, self.field.backward_key) == self.instance.id)
            .select(self.field.backward_key, self.field.forward_key)
        )
        query = db.query_class.into(through_table).columns(
            getattr(through_table, self.field.forward_key),
            getattr(through_table, self.field.backward_key),
        )

        if len(instances) == 1:
            criterion = getattr(through_table, self.field.forward_key) == instances[0].id
        else:
            criterion = getattr(through_table, self.field.forward_key).isin(
                [i.id for i in instances]
            )

        select_query = select_query.where(criterion)

        already_existing_relations_raw = await db.execute_query(str(select_query))
        already_existing_relations = {
            (r[self.field.backward_key], r[self.field.forward_key])
            for r in already_existing_relations_raw
        }

        insert_is_required = False
        for instance_to_add in instances:
            if instance_to_add.id is None:
                raise OperationalError(
                    "You should first call .save() on {model}".format(model=instance_to_add)
                )
            if (self.instance.id, instance_to_add.id) in already_existing_relations:
                continue
            query = query.insert(instance_to_add.id, self.instance.id)
            insert_is_required = True
        if insert_is_required:
            await db.execute_query(str(query))
1182800
def dict_update(base_dict, update_dict):
    
    if isinstance(update_dict, dict):
        base_dict.update(update_dict)

    return base_dict
749831
def update_fields(self, updates):
        

        for field in updates:
            if self._valid_field(field):
                self._track_changes.add(field)
            else:
                raise ValueError('"{}" is not a valid internal field name'.format(field))

        # Update existing instance of fields, or create a fields instance if needed
        if self.fields:
            self.fields.update(updates)
        else:
            self.fields = updates
758020
def dict_fields(obj, parent=[]):
    
    rtn_obj = {}
    for key, value in obj.items():
        new_key = parent + [key]
        new_key = ".".join(new_key)
        if isinstance(value, list):
            if value:
                value = value[0]
        if isinstance(value, dict):
            rtn_obj.update(dict_fields(value, [new_key]))
        else:
            rtn_obj.update({new_key: value})
    return rtn_obj
959107
def updated_current_fields(
            self,
            update_fields):
        
        self.fields_to_add = {}
        for k in self.org_fields:
            self.fields_to_add[k] = self.org_fields[k]
        self.fields_to_add.update(update_fields)
1007387
def insert_object(self, db_object):
        
        # Create object using the  to_dict() method.
        obj = self.to_dict(db_object)
        obj['active'] = True
        self.collection.insert_one(obj)