---
title: "Multiple object mixins"
version: 3.1
locale: ja
source: https://docs.djangoproject.com/ja/3.1/ref/class-based-views/mixins-multiple-object/
canonical: https://djangodocs.dev/ja/3.1/ref/class-based-views/mixins-multiple-object/
---
# Multiple object mixins

## `MultipleObjectMixin`

#### `class django.views.generic.list.MultipleObjectMixin`

オブジェクトのリストを表示するために使用する Mixin です。

`paginate_by` が指定された場合、Django は結果をページネートします。URL 内のページ数は以下のいずれかの方法で指定します:

- URLconf 内で `page` パラメータを使用します。例えば、URLconf は以下のようになります:

  ```
  path('objects/page<int:page>/', PaginatedView.as_view()),
  ```
- `page` クエリ文字列パラメータを通じて、ページ数を渡します。例えば、URL は以下のようになります:

  ```
  /objects/?page=3
  ```

これらの値は (0 ベースではなく) 1 ベースなので、最初のページはページ `1` で表示されます。

ページネーションの詳細については、[ページネーションのドキュメント](/ja/3.1/topics/pagination/) を参照してください。

特別なケースとして、`page` に対する値として `last` を使用することもできます:

```
/objects/?page=last
```

これによって、自分で何ページ存在するかを調べることなく、最後のページにアクセスすることができます。

注意すべきなのは、`page` は有効なページ数ないし値 `last` の *どちらかでなければならない* ことです。`page` に対する他の値はすべて 404 エラーとなります。

**Extends**

- [`django.views.generic.base.ContextMixin`](/ja/3.1/ref/class-based-views/mixins-simple/#django.views.generic.base.ContextMixin)

**メソッドと属性**

#### `allow_empty`

有効なオブジェクトが 1 つもない場合にページを表示するかどうかを指定する真偽値です。`False` に指定してオブジェクトが存在しない場合、空のページの代わりに 404 を投げます。デフォルトは `True` です。

#### `model`

このビューがデータを表示する対象のモデルです。`model = Foo` と指定することは、`queryset = Foo.objects.all()` の効率的な書き方で、 `objects` は `Foo` の [デフォルトマネージャ](/ja/3.1/topics/db/managers/#default-managers) を表します。

#### `queryset`

オブジェクトを表す `QuerySet` です。指定すると、`queryset` の値は [`model`](#django.views.generic.list.MultipleObjectMixin.model) の結果を上書きします。

> **Warning**
>
> `queryset` is a class attribute with a *mutable* value so care
> must be taken when using it directly. Before using it, either call
> its [`all()`](/ja/3.1/ref/models/querysets/#django.db.models.query.QuerySet.all) method or
> retrieve it with [`get_queryset()`](#django.views.generic.list.MultipleObjectMixin.get_queryset) which takes care of the
> cloning behind the scenes.

#### `ordering`

`queryset` に適用される並び順を指定するための文字列ないし文字列のリストです。[`order_by()`](/ja/3.1/ref/models/querysets/#django.db.models.query.QuerySet.order_by) に対するものと同じ値が有効です。

#### `paginate_by`

何個のオブジェクトが各ページに表示されるべきかを指定する数値です。この値が指定されると、各ページにおいて\`\`paginate\_by\`\` 数のオブジェクトをページネートします。ビューには (`request.GET` を通じた) `page` クエリ文字列パラメータ、もしくは URLconf で `page` 変数が指定されることを必要とします。

#### `paginate_orphans`

最後のページが含むことができる "はみ出た" オブジェクトの数を指定する数値です。最後のページで、[`paginate_by`](#django.views.generic.list.MultipleObjectMixin.paginate_by) の制限を最大で `paginate_orphans` の値まで拡張し、最後のページでほんの少しのオブジェクトしか表示されないことを防ぎます。

#### `page_kwarg`

ページのパラメータに使用する名前を指定する文字列です。ビューは (`request.GET` を通じた) クエリ文字列パラメータ、もしくはURLconf 内で指定した kwarg が有効になっていることを必要とします。デフォルトは `page` です。

#### `paginator_class`

The paginator class to be used for pagination. By default,
[`django.core.paginator.Paginator`](/ja/3.1/ref/paginator/#django.core.paginator.Paginator) is used. If the custom paginator
class doesn't have the same constructor interface as
[`django.core.paginator.Paginator`](/ja/3.1/ref/paginator/#django.core.paginator.Paginator), you will also need to
provide an implementation for [`get_paginator()`](#django.views.generic.list.MultipleObjectMixin.get_paginator).

#### `context_object_name`

Designates the name of the variable to use in the context.

#### `get_queryset()`

Get the list of items for this view. This must be an iterable and may
be a queryset (in which queryset-specific behavior will be enabled).

#### `get_ordering()`

Returns a string (or iterable of strings) that defines the ordering that
will be applied to the `queryset`.

Returns [`ordering`](#django.views.generic.list.MultipleObjectMixin.ordering) by default.

#### `paginate_queryset(queryset, page_size)`

Returns a 4-tuple containing (`paginator`, `page`, `object_list`,
`is_paginated`).

Constructed by paginating `queryset` into pages of size `page_size`.
If the request contains a `page` argument, either as a captured URL
argument or as a GET argument, `object_list` will correspond to the
objects from that page.

#### `get_paginate_by(queryset)`

Returns the number of items to paginate by, or `None` for no
pagination. By default this returns the value of [`paginate_by`](#django.views.generic.list.MultipleObjectMixin.paginate_by).

#### `get_paginator(queryset, per_page, orphans=0, allow_empty_first_page=True)`

Returns an instance of the paginator to use for this view. By default,
instantiates an instance of [`paginator_class`](#django.views.generic.list.MultipleObjectMixin.paginator_class).

#### `get_paginate_orphans()`

An integer specifying the number of "overflow" objects the last page
can contain. By default this returns the value of
[`paginate_orphans`](#django.views.generic.list.MultipleObjectMixin.paginate_orphans).

#### `get_allow_empty()`

Return a boolean specifying whether to display the page if no objects
are available. If this method returns `False` and no objects are
available, the view will raise a 404 instead of displaying an empty
page. By default, this is `True`.

#### `get_context_object_name(object_list)`

Return the context variable name that will be used to contain
the list of data that this view is manipulating. If
`object_list` is a queryset of Django objects and
[`context_object_name`](#django.views.generic.list.MultipleObjectMixin.context_object_name) is not set,
the context name will be the `model_name` of the model that
the queryset is composed from, with postfix `'_list'`
appended. For example, the model `Article` would have a
context object named `article_list`.

#### `get_context_data(**kwargs)`

Returns context data for displaying the list of objects.

**コンテキスト**

- `object_list`: The list of objects that this view is displaying. If
  `context_object_name` is specified, that variable will also be set
  in the context, with the same value as `object_list`.
- `is_paginated`: A boolean representing whether the results are
  paginated. Specifically, this is set to `False` if no page size has
  been specified, or if the available objects do not span multiple
  pages.
- `paginator`: An instance of
  [`django.core.paginator.Paginator`](/ja/3.1/ref/paginator/#django.core.paginator.Paginator). If the page is not
  paginated, this context variable will be `None`.
- `page_obj`: An instance of
  [`django.core.paginator.Page`](/ja/3.1/ref/paginator/#django.core.paginator.Page). If the page is not paginated,
  this context variable will be `None`.

## `MultipleObjectTemplateResponseMixin`

#### `class django.views.generic.list.MultipleObjectTemplateResponseMixin`

A mixin class that performs template-based response rendering for views
that operate upon a list of object instances. Requires that the view it is
mixed with provides `self.object_list`, the list of object instances that
the view is operating on. `self.object_list` may be, but is not required
to be, a [`QuerySet`](/ja/3.1/ref/models/querysets/#django.db.models.query.QuerySet).

**Extends**

- [`TemplateResponseMixin`](/ja/3.1/ref/class-based-views/mixins-simple/#django.views.generic.base.TemplateResponseMixin)

**メソッドと属性**

#### `template_name_suffix`

The suffix to append to the auto-generated candidate template name.
Default suffix is `_list`.

#### `get_template_names()`

Returns a list of candidate template names. Returns the following list:

- the value of `template_name` on the view (if provided)
- `<app_label>/<model_name><template_name_suffix>.html`
