LoginSignup
0
0

【Django】ORMのvalues_listメソッドの引数"flat=True"の使い方とは?サンプルコード解説

Posted at

概要

DjangoのORMではvalues_listメソッドがありますが、これの引数にはflatがあります。
本記事ではこのflatの意味と大事になるケースを紹介します。

サンプルコード

以下記事にて、以前紹介した下記のコードでflat=Trueを使っていたのですが、改めて使い方を確認。
【Python】終端記号を考慮して100文字までカットするコード

このコードでは、PuncCdモデルから取得した複数の終端記号の中で該当する最後のものにより文章を区切っています。

from xxx.models import PuncCd

def truncate_comment_at_last_punc(comment):
    last_punc_location = None
    punc_cd_values = PuncCd.objects.values_list('punc', flat=True)
    for punc_value in punc_cd_values:
        location = comment.rfind(punc_value)
        if location == -1:
            continue
        if last_punc_location is None or location > last_punc_location:
            last_punc_location = location

    truncated_comment = None
    if last_punc_location is not None:
        truncated_comment = comment[:last_punc_location + 1]

    return truncated_comment

comment = "ここにコメントが入ります"
comment_100 = truncate_comment_at_last_punc(comment[:100])

上記で、もしflat=Trueとしない場合、以下のエラーになります。

'must be str, not tuple' 

なぜかというと、rfindメソッドは文字列に対して動作するため、タプルではなく文字列を渡す必要があります。しかし、values_listflat=Trueを使用しない場合、punc_cd_valuesはタプルのリストであり、タプルは文字列ではないため、rfindメソッドがタプルに対して呼ばれると 'must be str, not tuple'エラーが発生するのです。

flat=Trueとは?

Django ORM(Object-Relational Mapping)のvalues_listメソッドに対する引数。
この引数を指定することで、values_listメソッドはクエリセットから取得したデータを平らな(フラットな)リストとして返します。
リストとして扱いたい場合に活用できます。

例としては、以下のようになります。

flat=Trueの場合

country = Country.objects.values_list("country_name", flat=True)
print(country)

# 出力
# <QuerySet ['日本', 'スペイン', 'ドイツ', 'コスタリカ']>

flat=Falseの場合

country = Country.objects.values_list("country_name", flat=False)
print(country)

# 出力
# <QuerySet [('日本',), ('スペイン',), ('ドイツ',), ('コスタリカ',)]>

上記のように、Falseにするとそれぞれのオブジェクトがタプルの要素となっていることがわかります。
各値をタプル内にラップされたリストにしたい時は、Falseが使えます。この場合は、値にアクセスするためにインデックスを使用する必要があります。

逆に、単純な値のリストが得たい場合はTrueを使うと良いでしょう。
Trueにすることで、各カラムの値がリストの要素として一括りになります。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0