django.shortcuts._get_queryset - python examples

Here are the examples of the python api django.shortcuts._get_queryset taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.

5 Examples 7

3 View Complete Implementation : utils.py
Copyright MIT License
Author : ic-labs
def get_visible_object_or_404(klast, *args, **kwargs):
    """
    Convenience replacement for `get_object_or_404` that automatically finds
    and returns only the *visible* copy of publishable items, or raises
    `Http404` if a visible copy is not available even when a draft copy is
    available.
    """
    qs = _get_queryset(klast)
    # If clast is publishable, find only *visible* objects
    try:
        qs = qs.visible()
    except AttributeError:
        past  # Ignore error calling `visible()` on unpublishable clast
    return get_object_or_404(qs, *args, **kwargs)

3 View Complete Implementation : histogram.py
Copyright MIT License
Author : martsberger
def simple_histogram(queryset, column, bins):
    """
    Return a histogram from data in queryset.

    :param queryset: A Queryet, Model, or Manager
    :param column: The column we are aggregating into a histogram
    :param bins: An ordered iterable of left endpoints of the bins. Must have at least two elements.
    The endpoints must be a convertible to strings by force_text
    :return: A dictionary with bin endpoints converted to strings as keys and
    """
    queryset = _get_queryset(queryset)

    queryset = queryset.annotate(column_name=Value(column, output_field=CharField()))

    return multi_histogram(queryset, column, bins, slice_on='column_name', choices=((column, column),))

3 View Complete Implementation : __init__.py
Copyright The Unlicense
Author : nerosketch
def get_object_or_None(klast, *args, **kwargs):
    queryset = _get_queryset(klast)
    try:
        return queryset.get(*args, **kwargs)
    except AttributeError:
        klast__name = klast.__name__ if isinstance(klast, type) else klast.__clast__.__name__
        raise ValueError(
            "First argument to get_object_or_404() must be a Model, Manager, "
            "or QuerySet, not '%s'." % klast__name
        )
    except queryset.model.DoesNotExist:
        return

0 View Complete Implementation : histogram.py
Copyright MIT License
Author : martsberger
def multi_histogram(queryset, column, bins, slice_on, choices):
    """
    Returns a table of histograms, one for each unique value of field in queryset.

    :param queryset:  A Queryet, Model, or Manager
    :param column: The column we are aggregating into a histogram
    :param bins: An ordered iterable of left endpoints of the bins. Must have at least two elements.
    The endpoints must be a convertible to strings by force_text
    :param slice_on: A field of the queryset that we are slicing the histograms on
    :return: A ValuesQuerySet
    """
    queryset = _get_queryset(queryset)

    field_values = get_column_values(queryset, slice_on, choices)

    bins = [force_text(bin) for bin in bins]

    whens = tuple(
        between_include_start(column, bins[k], bins[k+1], Value(force_text(bins[k])))
        for k in range(len(bins) - 1)
    ) + (
        When(Q(**{column + '__gte': bins[-1]}), Value(force_text(bins[-1]))),
    )

    ordering_whens = tuple(
        between_include_start(column, bins[k], bins[k + 1], Value(k))
        for k in range(len(bins) - 1)
    ) + (
        When(Q(**{column + '__gte': bins[-1]}), Value(len(bins) - 1)),
    )

    bin_annotation = {
        'bin': Case(*whens, output_field=CharField()),
        'order': Case(*ordering_whens, output_field=IntegerField())
    }

    histogram_annotation = {
        display_value: Count(Case(When(Q(**{slice_on: field_value}), then=1), output_field=IntegerField()))
        for field_value, display_value in field_values
    }

    qs = queryset.annotate(**bin_annotation).order_by('order').values('bin').filter(bin__isnull=False).annotate(**histogram_annotation)

    return _zero_fill(qs, bins, field_values)

0 View Complete Implementation : pivot.py
Copyright MIT License
Author : martsberger
def pivot(queryset, rows, column, data, aggregation=Sum, choices='auto', display_transform=lambda s: s, default=None, row_range=()):
    """
    Takes a queryset and pivots it. The result is a table with one record
    per unique value in the `row` column, a column for each unique value in the `column` column
    and values in the table aggregated by the data column.

    :param queryset: a QuerySet, Model, or Manager
    :param rows: list of strings, name of columns that will key the rows
    :param column: string, name of column that will define columns
    :param data: column name or Combinable
    :param aggregation: aggregation function to apply to data column
    :param display_transform: function that takes a string and returns a string
    :param default: default value to past to the aggregate function when no record is found
    :param row_range: iterable with the expected range of rows in the result
    :return: ValuesQueryset
    """
    values = [rows] if isinstance(rows, six.string_types) else list(rows)

    queryset = _get_queryset(queryset)

    column_values = get_column_values(queryset, column, choices)

    annotations = _get_annotations(column, column_values, data, aggregation, display_transform, default=default)
    for row in values:
        row_choices = get_field_choices(queryset, row)
        if row_choices:
            whens = (When(Q(**{row: value}), then=Value(display_value, output_field=CharField())) for value, display_value in row_choices)
            row_display = Case(*whens)
            queryset = queryset.annotate(**{'get_' + row + '_display': row_display})
            values.append('get_' + row + '_display')

    values_list = queryset.values(*values).annotate(**annotations)

    if row_range:
        attributes = [value[0] for value in column_values]
        values_list = default_fill(values_list, values[0], row_range, fill_value=default, fill_attributes=attributes)

    return values_list