... regate functions . See the Func documentation for security considerations. 各関数の例では、以下のモデルを使用します: class Author ( models . Model ): name = models . CharField ( max_length = 50 ) age = models . PositiveIntegerField ( null = True , blan ... 強制的に変更します。 使用例: >>> from django.db.models import FloatField >>> from django.db.models.functions import Cast >>> Author . objects . create ( age = 25 , name = "Margaret Smith" ) >>> author = Author . objects . annotate ( ... age_as_float = Ca ... st ( "age" , output_field = FloatField ()), ... ) . get () >>> print ( author . age_as_float ) 25.0 Coalesce ¶ class Coalesce ( * expressions , ** extra ) [ソース] ¶ 少なくとも2つのフィールド名または式 ... ... regate functions . See the Func documentation for security considerations. 各関数の例では、以下のモデルを使用します: class Author ( models . Model ): name = models . CharField ( max_length = 50 ) age = models . PositiveIntegerField ( null = True , blan ... 強制的に変更します。 使用例: >>> from django.db.models import FloatField >>> from django.db.models.functions import Cast >>> Author . objects . create ( age = 25 , name = "Margaret Smith" ) >>> author = Author . objects . annotate ( ... age_as_float = Ca ... st ( "age" , output_field = FloatField ()), ... ) . get () >>> print ( author . age_as_float ) 25.0 Coalesce ¶ class Coalesce ( * expressions , ** extra ) [ソース] ¶ 少なくとも2つのフィールド名または式 ... ... import models from django.forms import ModelForm TITLE_CHOICES = { "MR" : "Mr." , "MRS" : "Mrs." , "MS" : "Ms." , } class Author ( models . Model ): name = models . CharField ( max_length = 100 ) title = models . CharField ( max_length = 3 , choices = ... e ) def __str__ ( self ): return self . name class Book ( models . Model ): name = models . CharField ( max_length = 100 ) authors = models . ManyToManyField ( Author ) class AuthorForm ( ModelForm ): class Meta : model = Author fields = [ "name" , "ti ... tle" , "birth_date" ] class BookForm ( ModelForm ): class Meta : model = Book fields = [ "name" , "authors" ] これらのモデルでは、上記の ModelForm サブクラスはおおむね以下と同等と言えます (唯一の違いは save() メソッドで、これから ... ... import models from django.forms import ModelForm TITLE_CHOICES = { "MR" : "Mr." , "MRS" : "Mrs." , "MS" : "Ms." , } class Author ( models . Model ): name = models . CharField ( max_length = 100 ) title = models . CharField ( max_length = 3 , choices = ... e ) def __str__ ( self ): return self . name class Book ( models . Model ): name = models . CharField ( max_length = 100 ) authors = models . ManyToManyField ( Author ) class AuthorForm ( ModelForm ): class Meta : model = Author fields = [ "name" , "ti ... tle" , "birth_date" ] class BookForm ( ModelForm ): class Meta : model = Book fields = [ "name" , "authors" ] これらのモデルでは、上記の ModelForm サブクラスはおおむね以下と同等と言えます (唯一の違いは save() メソッドで、これから ... ... """ Returns the feed's description as a normal Python string. """ description = "Foo bar baz." # Hard-coded description. # AUTHOR NAME --One of the following three is optional. The framework # looks for them in this order. def author_name ( self , obj ... ): """ Takes the object returned by get_object() and returns the feed's author's name as a normal Python string. """ def author_name ( self ): """ Returns the feed's author's name as a normal Python st ... ring. """ author_name = "Sally Smith" # Hard-coded author name. # AUTHOR EMAIL --One of the following three is optional. The framework # lo ... ... """ Returns the feed's description as a normal Python string. """ description = "Foo bar baz." # Hard-coded description. # AUTHOR NAME --One of the following three is optional. The framework # looks for them in this order. def author_name ( self , obj ... ): """ Takes the object returned by get_object() and returns the feed's author's name as a normal Python string. """ def author_name ( self ): """ Returns the feed's author's name as a normal Python st ... ring. """ author_name = "Sally Smith" # Hard-coded author name. # AUTHOR EMAIL --One of the following three is optional. The framework # lo ... ... 合、 form_class が ModelForm だったとしても、モデルを指定する必要があります。 First we need to add get_absolute_url() to our Author class: models.py ¶ from django.db import models from django.urls import reverse class Author ( models . Model ): name = mo ... dels . CharField ( max_length = 200 ) def get_absolute_url ( self ): return reverse ( "author-detail" , kwargs = { "pk" : self . pk }) そうしたら、 CreateView およびその仲間たちを使って実際に動作させることができます。 ... ls import reverse_lazy from django.views.generic.edit import CreateView , DeleteView , UpdateView from myapp.models import Author class AuthorCreateView ( CreateView ): model = Author fields = [ "name" ] class AuthorUpdateView ( UpdateView ): model = A ... ... 合、 form_class が ModelForm だったとしても、モデルを指定する必要があります。 First we need to add get_absolute_url() to our Author class: models.py ¶ from django.db import models from django.urls import reverse class Author ( models . Model ): name = mo ... dels . CharField ( max_length = 200 ) def get_absolute_url ( self ): return reverse ( "author-detail" , kwargs = { "pk" : self . pk }) そうしたら、 CreateView およびその仲間たちを使って実際に動作させることができます。 ... ls import reverse_lazy from django.views.generic.edit import CreateView , DeleteView , UpdateView from myapp.models import Author class AuthorCreateView ( CreateView ): model = Author fields = [ "name" ] class AuthorUpdateView ( UpdateView ): model = A ... ... SuccessMessageMixin が含まれており、フォーム送信の成功に関するメッセージを表示できます。 注釈 このページの例のいくつかは、 Author モデルが下記の通り myapp/models.py 内で定義されているものと見なしています。 from django.db import models from django.urls ... import reverse class Author ( models . Model ): name = models . CharField ( max_length = 200 ) def get_absolute_url ( self ): return reverse ( "author ... ストに描画された CreateView のページは、 '_form' の template_name_suffix を使います。 例えば、この属性をオブジェクト (例えば Author モデル) を作成するビューのために '_create_form' に変更すると、 デフォルトの template_name は 'myapp/author_create_form.htm ... ... SuccessMessageMixin が含まれており、フォーム送信の成功に関するメッセージを表示できます。 注釈 このページの例のいくつかは、 Author モデルが下記の通り myapp/models.py 内で定義されているものと見なしています。 from django.db import models from django.urls ... import reverse class Author ( models . Model ): name = models . CharField ( max_length = 200 ) def get_absolute_url ( self ): return reverse ( "author ... ストに描画された CreateView のページは、 '_form' の template_name_suffix を使います。 例えば、この属性をオブジェクト (例えば Author モデル) を作成するビューのために '_create_form' に変更すると、 デフォルトの template_name は 'myapp/author_create_form.htm ... 2026年10月08日