... ue は null=False のフィールドでも使用できますが、この場合、 clean() を実装し、欠損値をプログラムで補完する必要があります。 choices Field. choices [ソース] このフィールドの選択肢として使用するための、以下で説明する形式のマッピングまたはイテラブルです。ch ... マッピングが指定された場合、キー要素はモデルに設定される実際の値で、2番目の要素は人間が読める名前です。例えば YEAR_IN_SCHOOL_CHOICES = { "FR" : "Freshman" , "SO" : "Sophomore" , "JR" : "Junior" , "SR" : "Senior" , "GR" : "Graduate" , } また、 sequence とし ... B2), ...] )。各タプルの最初の要素はモデルに設定される実際の値で、2番目の要素は人間が読める名前です。例えば YEAR_IN_SCHOOL_CHOICES = [ ( "FR" , "Freshman" ), ( "SO" , "Sophomore" ), ( "JR" , "Junior" ), ( "SR" , "Senior" ), ( "GR" , "Graduate" ), ] また ... ... , output_field = models . BigIntegerField (), db_persist = True , ) フィールドの選択肢を宣言する際のオプションが増加 Field.choices (モデルフィールド用) と ChoiceField.choices (フォームフィールド用) により、値を宣言する際の柔軟性が向上しました。以前のバー ... ジョンの Django では、 choices は 2 タプルのリストか、 列挙型 のサブクラスである必要がありましたが、後者の場合、 .choices 属性にアクセスして、期待される形 ... 式で値を提供する必要がありました: from django.db import models Medal = models . TextChoices ( "Medal" , "GOLD SILVER BRONZE" ) SPORT_CHOICES = [ ( "Martial Arts" , [( "judo" , "Judo" ), ( "karate" , "Karate" )]), ( ... ... エラーメッセージには %(value)s が含まれていることがあり、選択された選択肢に置き換えられます。 1つの追加引数を受け取ります: choices [ソース] このフィールドの選択肢として使用する2値タプルの iterable 、 列挙型 、またはそのようなイテラブルを返す呼び出し可能 ... オブジェクトのいずれか。この引数は、モデルフィールドの choices 引数と同じ形式を受け入れます。詳細については、 モデルフィールドのリファレンスドキュメント を参照してください。引数が呼び出 ... ecified location should be included. Either this or allow_files must be True . FilePathField has the following method: set_choices () [ソース] New in Django 6.1. Scans the directory at path and refreshes the field's choices. This is called automatically ... ... 際にセットできます。以下の例では、 SelectDateWidget に対して years 属性がセットされます: from django import forms BIRTH_YEAR_CHOICES = [ "1980" , "1981" , "1982" ] FAVORITE_COLORS_CHOICES = { "blue" : "Blue" , "green" : "Green" , "black" : "Black" , } cla ... ss SimpleForm ( forms . Form ): birth_year = forms . DateField ( widget = forms . SelectDateWidget ( years = BIRTH_YEAR_CHOICES ) ) favorite_colors = forms . MultipleChoiceField ( required = False , widget = forms . CheckboxSelectMultiple , choices = ... FAVORITE_COLORS_CHOICES , ) 使用可能なウィジェットとそれぞれの引数の詳細については、 ビルトインのウィジェット を参照してください。 ウィジェットのイ ... ... ng to the admin index page. The admin's FilteredSelectMultiple widget now uses <optgroup> s to preserve named groups (e.g. choices=[("Group", [("1", "Item")]), ...] ). When ModelAdmin.list_select_related is False (the default), the change list now selec ... CE_LABEL defines a more accessible and translatable default label for the blank choice in forms, which is appended to most choices lists. The transitional setting USE_BLANK_CHOICE_DASH allows you to revert back to the old default label. FilePathField no ... w provides a set_choices() method to scan the directory at path and refresh the field's choices. This allows per-request refreshing when called in ... ... worked. >>> q = Question . objects . get ( pk = 1 ) >>> q . was_published_recently () True # Give the Question a couple of Choices. The create call constructs a new # Choice object, does the INSERT statement, adds the choice to the set # of available ch ... on (e.g. a question's choice) which can be accessed via the API. >>> q = Question . objects . get ( pk = 1 ) # Display any choices from the related object set -- none so far. >>> q . choice_set . all () <QuerySet []> # Create three choices. >>> q . choi ... e double underscores to separate relationships. # This works as many levels deep as you want; there's no limit. # Find all Choices for any question whose pub_date is in this year # (reusing the 'current_year' variable we created above). >>> Choice . obj ... ... たものがセットされます。 フォームフィールドの help_text はモデルフィールドの help_text がセットされます。 モデルフィールドに choices が設定されている場合、フォームフィールドの widget は Select にセットされ、選択肢にはモデルフィールドの choices が使われます ... な具体例 以下のような一連のモデルを考えていきましょう: from django.db import models from django.forms import ModelForm TITLE_CHOICES = { "MR" : "Mr." , "MRS" : "Mrs." , "MS" : "Ms." , } class Author ( models . Model ): name = models . CharField ( max_leng ... th = 100 ) title = models . CharField ( max_length = 3 , choices = TITLE_CHOICES ) birth_date = models . DateField ( blank = True , null = True ) def __str__ ( self ): return self . name ... ... StoreField when using on expressions with params ( #30672 ). Fixed a regression in Django 2.2 where ModelAdmin.list_filter choices to foreign objects don't respect a model's Meta.ordering ( #30449 ). ... ... 用します: from django.db import models class Client ( models . Model ): REGULAR = "R" GOLD = "G" PLATINUM = "P" ACCOUNT_TYPE_CHOICES = { REGULAR : "Regular" , GOLD : "Gold" , PLATINUM : "Platinum" , } name = models . CharField ( max_length = 50 ) register ... ed_on = models . DateField () account_type = models . CharField ( max_length = 1 , choices = ACCOUNT_TYPE_CHOICES , default = REGULAR , ) When class When ( condition = None , then = None , ** lookups ) [ソース] Wh ... ... in QuerySet filters, without having to first annotate and then filter against the annotation. Enumerations for model field choices Custom enumeration types TextChoices , IntegerChoices , and Choices are now available as a way to define Field.choices . T ... extChoices and IntegerChoices types are provided for text and integer fields. The Choices class allows defining a compatible enumerat ... 2026年10月08日