Esempio n. 1
0
def related_field_paths(
    manager,
    related_model,
    exclude_field_types=(),
    field_names=None,
    excludes=[],
    separator=None,
    for_value_queryset=False,
):
    """
        Iterates through the fields of the related model, appending each to the related model field name
        from the main model. Returns the list of related field paths.

        if for_values_query_set=True, we use attname instead of the name of the field. This gives us the _id version of Foreign keys,
        which is what we want assuming we're created a values_query_set, since the the value will always
        be the related instance id and not the instance itself
    """
    related_field = resolve_related_model_path_via_geographies(manager, related_model)
    return compact(
        map(
            lambda field: string.replace(
                "{0}__{1}".format(related_field, field.attname if for_value_queryset else field.name),
                "__",
                separator or "__",
            )
            if field_predicate(field, exclude_field_types, field_names, excludes)
            else None,
            # Sort by the field's position in fields if fields is defined
            sorted(related_model._meta.fields, key=index_of_field_for_sort(field_names, for_value_queryset)),
        )
    )
Esempio n. 2
0
def related_field_paths(manager,
                        related_model,
                        exclude_field_types=(),
                        field_names=None,
                        excludes=[],
                        separator=None,
                        for_value_queryset=False):
    """
        Iterates through the fields of the related model, appending each to the related model field name
        from the main model. Returns the list of related field paths.

        if for_values_query_set=True, we use attname instead of the name of the field. This gives us the _id version of Foreign keys,
        which is what we want assuming we're created a values_query_set, since the the value will always
        be the related instance id and not the instance itself
    """
    related_field = resolve_related_model_path_via_geographies(
        manager, related_model)
    return compact(map(
        lambda field: string.replace(
            '{0}__{1}'.format(related_field, field.attname if for_value_queryset else field.name),
            '__',
            separator or '__') if \
                field_predicate(field, exclude_field_types, field_names, excludes) else \
                None,
        # Sort by the field's position in fields if fields is defined
        sorted(
            related_model._meta.fields,
            key=index_of_field_for_sort(field_names, for_value_queryset)
    )))
Esempio n. 3
0
def related_field_paths_to_fields(manager, related_model, exclude_field_types=(), fields=None, excludes=[], separator=None):
    """
        Iterates through the fields of the related model, appending each to the related model field name
        from the main model. Returns the dict of related field paths as keys valued by the field.
        :param exclude_field_types. Optional tuple of Field classes that should be filtered out.
        :param separator: Optional separator with which to replace __ in the related_field_paths
    """
    related_field = resolve_related_model_path_via_geographies(manager, related_model)
    return map_to_dict(
        lambda field: _field_path_and_cloned_field(related_field,field, separator) if \
            field_predicate(field, exclude_field_types, fields, excludes) else None,
        related_model._meta.fields)
Esempio n. 4
0
def related_field_paths_to_fields(
    manager, related_model, exclude_field_types=(), fields=None, excludes=[], separator=None
):
    """
        Iterates through the fields of the related model, appending each to the related model field name
        from the main model. Returns the dict of related field paths as keys valued by the field.
        :param exclude_field_types. Optional tuple of Field classes that should be filtered out.
        :param separator: Optional separator with which to replace __ in the related_field_paths
    """
    related_field = resolve_related_model_path_via_geographies(manager, related_model)
    return map_to_dict(
        lambda field: _field_path_and_cloned_field(related_field, field, separator)
        if field_predicate(field, exclude_field_types, fields, excludes)
        else None,
        related_model._meta.fields,
    )