Non-foreign key fields can be mapped to new article instances
    directly. Foreign key fields require a bit more work.
    This method returns a tuple, of the same format:
    (flat fields, nested fields)
19885
def separate_fields(fields):
    
    flat_fields = {}
    nested_fields = {}

    # exclude "id" and "meta" elements
    for k, v in fields.items():
        # TODO: remove dependence on KEYS_TO_INCLUDE
        if k not in KEYS_TO_EXCLUDE:
            if type(v) not in [type({}), type([])]:
                flat_fields.update({k: v})
            else:
                nested_fields.update({k: v})

    return flat_fields, nested_fields
706378
def get_natural_key_fields(cls):
        
        natural_key = []
        for name, rel_to in cls.get_natural_key_info():
            if not rel_to:
                natural_key.append(name)
            else:
                nested_key = rel_to.get_natural_key_fields()
                natural_key.extend([
                    name + '__' + nname
                    for nname in nested_key
                ])
        return natural_key
1078837
def formfield_for_dbfield(self, db_field, **kwargs):
        
        if isinstance(db_field, models.ForeignKey) and db_field.name in self.related_search_fields:
            help_text = self.get_help_text(db_field.name, db_field.remote_field.model._meta.object_name)
            if kwargs.get('help_text'):
                help_text = six.u('%s %s' % (kwargs['help_text'], help_text))
            kwargs['widget'] = ForeignKeySearchInput(db_field.remote_field, self.related_search_fields[db_field.name])
            kwargs['help_text'] = help_text
        return super(ForeignKeyAutocompleteAdminMixin, self).formfield_for_dbfield(db_field, **kwargs)
1221817
def field_to_dict(field, instance):
    
    # avoid a circular import
    from django.db.models.fields.related import ManyToManyField

    return (many_to_many_field_to_dict(field, instance) if isinstance(field, ManyToManyField)
            else field.value_from_object(instance))
215856
def _get_simple_fields(self):
        
        fields = []
        for field_name in self.cls._fields:
            field = self.cls.get_field(field_name)
            if not isinstance(field, MultiValuesField):
                fields.append(field_name)
        return fields
736678
def tuple(data, field_name):
    
    if isinstance(data, Cube):
        Log.error("not supported yet")

    if isinstance(data, FlatList):
        Log.error("not supported yet")

    if is_data(field_name) and "value" in field_name:
        # SIMPLIFY {"value":value} AS STRING
        field_name = field_name["value"]

    # SIMPLE PYTHON ITERABLE ASSUMED
    if is_text(field_name):
        if len(split_field(field_name)) == 1:
            return [(d[field_name],) for d in data]
        else:
            path = split_field(field_name)
            output = []
            flat_list._tuple1(data, path, 0, output)
            return output
    elif is_list(field_name):
        paths = [_select_a_field(f) for f in field_name]
        output = FlatList()
        _tuple((), unwrap(data), paths, 0, output)
        return output
    else:
        paths = [_select_a_field(field_name)]
        output = FlatList()
        _tuple((), data, paths, 0, output)
        return output
1137807
def field_dict(self, model):
        
        if self.field_filter:
            return dict(
                [(f.name, f) for f in model._meta.fields
                 if self.field_filter(f)]
            )
        else:
            return dict(
                [(f.name, f) for f in model._meta.fields
                 if not f.rel and
                    not f.primary_key and
                    not f.unique and
                    not isinstance(f, (models.AutoField, models.TextField))]
            )
156068
def _expand_group_by_fields(cls, model, fields):
        
        # Containers for resulting fields and related model fields
        res = []
        related = {}

        # Add own fields and populate related fields
        for field_name in fields:
            if '__' in field_name:
                # Related model field: append to related model's fields
                fk_field_name, related_field = field_name.split('__', 1)
                if fk_field_name not in related:
                    related[fk_field_name] = [related_field]
                else:
                    related[fk_field_name].append(related_field)

            else:
                # Simple field, get the field instance
                model_field = model._meta.get_field(field_name)

                if isinstance(model_field, (ForeignKey, ManyToManyField)):
                    # It's a related field, get model
                    related_model = model_field.related_model

                    # Append all its fields with the correct prefix
                    res.extend('{}__{}'.format(field_name, f.column)
                               for f in related_model._meta.fields)

                else:
                    # It's a common field, just append it
                    res.append(field_name)

        # Resolve all related fields
        for fk_field_name, field_names in related.items():
            # Get field
            fk = model._meta.get_field(fk_field_name)

            # Get all fields for that related model
            related_fields = cls._expand_group_by_fields(fk.related_model,
                                                         field_names)

            # Append them with the correct prefix
            res.extend('{}__{}'.format(fk_field_name, f) for f in related_fields)

        # Return all fields
        return res
470884
def get_columns(model=None, fields=None, meta=None):
    
    if model:
        M = get_model(model)
    else:
        M = None
    if fields is not None:
        f = fields

    if M:
        if meta and hasattr(M, meta):
            m = getattr(model, meta)
            if hasattr(m, 'fields'):
                f = m.fields
            else:
                f = M._fields_list
        else:
            f = M._fields_list

    columns = []
    for x in f:
        if isinstance(x, str):  # so x is field_name
            field_name = x
        elif isinstance(x, dict):
            field_name = x['name']
        else:
            raise UliwebError("Field definition is not right, it should be just like str or {'name':xxx}")

        if '.' in field_name:
            model_name, field_name = field_name.split('.')
            M = get_model(model_name)

        if not M:
            raise UliwebError("Model can't be empty, because field name not has `model.` prefix")

        if field_name in M.c:
            columns.append(M.c[field_name])

    return columns
1192129
def to_named_tuple(obj, visited_children=None, back_relationships=None, levels=None, ignore=[], extras={}):
    

    if visited_children is None:
        visited_children = []

    if back_relationships is None:
        back_relationships = []

    serialized_data = {c.key: getattr(obj, c.key) for c in obj.__table__.columns}


    #Any other non-column data to include in the keyed tuple
    for k, v in extras.items():
        serialized_data[k] = v

    relationships = class_mapper(obj.__class__).relationships

    #Set the attributes to 'None' first, so the attributes are there, even if they don't
    #get filled in:
    for name, relation in relationships.items():
        if relation.uselist:
            serialized_data[name] = tuple([])
        else:
            serialized_data[name] = None


    visitable_relationships = [(name, rel) for name, rel in relationships.items() if name not in back_relationships]

    if levels is not None and levels > 0:
        for name, relation in visitable_relationships:

            levels = levels - 1

            if name in ignore:
                continue

            if relation.backref:
                back_relationships.append(relation.backref)

            relationship_children = getattr(obj, name)

            if relationship_children is not None:
                if relation.uselist:
                    children = []
                    for child in [c for c in relationship_children if c not in visited_children]:
                        visited_children.append(child)
                        children.append(to_named_tuple(child, visited_children, back_relationships, ignore=ignore, levels=levels))
                    serialized_data[name] = tuple(children)
                else:
                    serialized_data[name] = to_named_tuple(relationship_children, visited_children, back_relationships, ignore=ignore, levels=levels)

    vals = []
    cols = []
    for k, v in serialized_data.items():
        vals.append(k)
        cols.append(v)

    result = KeyedTuple(cols, vals)

    return result
276523
def get_field_info(model):
    
    # Deal with the primary key.
    if issubclass(model, mongoengine.EmbeddedDocument):
        pk = None
    else:
        pk = model._fields[model._meta['id_field']]

    # Deal with regular fields.
    fields = OrderedDict()

    # Deal with forward relationships.
    # Pass forward relations since there is no relations on mongodb
    references = OrderedDict()

    embedded = OrderedDict()

    def add_field(name, field):
        if isinstance(field, REFERENCING_FIELD_TYPES):
            references[name] = get_relation_info(field)
        elif isinstance(field, EMBEDDING_FIELD_TYPES):
            embedded[name] = get_relation_info(field)
        elif isinstance(field, COMPOUND_FIELD_TYPES):
            fields[name] = field
            if field.field:
                add_field(name + '.child', field.field)
        elif field is pk:
            return
        else:
            fields[name] = field

    for field_name in model._fields_ordered:
        add_field(field_name, model._fields[field_name])

    # Shortcut that merges both regular fields and the pk,
    # for simplifying regular field lookup.
    fields_and_pk = OrderedDict()
    fields_and_pk['pk'] = pk
    fields_and_pk[getattr(pk, 'name', 'pk')] = pk
    fields_and_pk.update(fields)

    return FieldInfo(pk,
                     fields,
                     references,
                     fields_and_pk,
                     embedded)
932396
protected function _getField() {
        //We can access to a field of the current Entity or a linked Entity (foreign key)
        //If the field name contain a "." it's the second case
        $fields = [];
        $fieldsData = $this->_entity->fields();

        //We have to analyze a classic field without linked Entity
        if (!preg_match('#\.#isU', $this->_field)) {
            if (isset($fieldsData[$this->_field])) {
                $fields = [$fieldsData[$this->_field]->value];
            }
            else {
                throw new MissingEntityException('The field "' . $this->_field . '" in the Entity "' . $this->_entity->name() . '" does\'nt exist');
            }
        }
        else {
            //Here it's sure that we have a linked Entity. But we don't compute "One to One and One to Many" in the same way than the 2 other
            //because in the two first the field value is an Entity object and in the two other, the field value is a Collection
            //To know which type of linked Entity it's, we just split the name on the "." and we take the index 0 of the generated array

            $entityName = explode('.', $this->_field);

            if (array_key_exists($entityName[0], $fieldsData) && $fieldsData[$entityName[0]]->foreign != null) {
                /** @var int $foreignType : the type of the foreign key */
                $foreignType = $fieldsData[$entityName[0]]->foreign->type();
                /** @var mixed $fieldValue : the value of the sub-field from the Entity (post.article.content->value) */
                $fieldValue = $fieldsData[$entityName[0]]->value->fields()[$entityName[1]];

                if ($foreignType == ForeignKey::ONE_TO_MANY || $foreignType == ForeignKey::MANY_TO_ONE) {
                    $fields = $this->_getForeignKeyFieldValue($fieldValue);
                }
                else { //We must get all the sub entities as an array an then loop through this array to get the field values
                    foreach ($fieldValue->data() as $fieldValueCollection) {
                        $fields = array_merge($fields, $this->_getForeignKeyFieldValue($fieldValueCollection));
                    }
                }
            }
            else {
                throw new MissingEntityException('The field "' . $this->_field . '" in the Entity "' . $this->_entity->name() . '" does\'nt exist');
            }
        }

        return $fields;
    }
120529
def _get_fields(self, table_name, **kwargs):
        """"""
        ret = {}
        query_str = 'PRAGMA table_info({})'.format(self._normalize_table_name(table_name))
        fields = self._query(query_str, **kwargs)
        #pout.v([dict(d) for d in fields])

        query_str = 'PRAGMA foreign_key_list({})'.format(self._normalize_table_name(table_name))
        fks = {f["from"]: f for f in self._query(query_str, **kwargs)}
        #pout.v([dict(d) for d in fks.values()])

        pg_types = {
            "INTEGER": int,
            "BIGINT": long,
            "DOUBLE PRECISION": float,
            "FLOAT": float,
            "REAL": float,
            "NUMERIC": decimal.Decimal,
            "BOOLEAN": bool,
            "DATE": datetime.date,
            "TIMESTAMP": datetime.datetime,
            "CHARACTER": str,
            "VARCHAR": str,
            "TEXT": str,
            "BLOB": bytearray,
        }

        # the rows we can set: field_type, name, field_required, min_size, max_size,
        #   size, unique, pk, <foreign key info>
        # These keys will roughly correspond with schema.Field
        # TODO -- we could actually use "type" to get the size because SQLite returns
        # a value like VARCHAR[32]
        for row in fields:
            field = {
                "name": row["name"],
                "field_required": bool(row["notnull"]) or bool(row["pk"]),
                "pk": bool(row["pk"]),
            }

            for tname, ty in pg_types.items():
                if row["type"].startswith(tname):
                    field["field_type"] = ty
                    break

            if field["pk"] and field["field_type"] is int:
                # we compensate for SQLite internally setting pk to int
                field["field_type"] = long

            if row["name"] in fks:
                field["schema_table_name"] = fks[row["name"]]["table"]
                field["ref_table_name"] = fks[row["name"]]["table"]

            ret[field["name"]] = field

        return ret
267958
def copy_fields(src, to):
    
    args = tuple(getattr(src, field.attname) for field in src._meta.fields)
    return to(*args)
462596
def initial_value(self, field_name: str = None):
        
        if self._meta.get_field(field_name).get_internal_type() == 'ForeignKey':
            if not field_name.endswith('_id'):
                field_name = field_name+'_id'

        attribute = self._diff_with_initial.get(field_name, None)

        if not attribute:
            return None

        return attribute[0]
356089
def get_flattened_bsp_keys_from_schema(schema):
    
    keys = []

    for key in schema.declared_fields.keys():
        field = schema.declared_fields[key]

        if isinstance(field, mm.fields.Nested) and \
                isinstance(field.schema, BoundSpatialPoint):
            keys.append("{}.{}".format(key, "position"))

    return keys
990571
def copy(self):
        
        inst = super(type(self), type(self)).empty(**self.dimensions)
        
        # Need to copy all attributes, fields, relations
        inst.__attributes__ = {k: v.copy() for k, v in self.__attributes__.items()}
        inst.__fields__ = {k: v.copy() for k, v in self.__fields__.items()}
        inst.__relations__ = {k: v.copy() for k, v in self.__relations__.items()}
        inst.maps = {k: m.copy() for k, m in self.maps.items()}
        inst.dimensions = self.dimensions.copy()
        
        return inst