... 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() メソッドで、これから ... ... """ 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 = mode ... ls . 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 ... ... mode> . Django provides three fetch modes. We'll explain them below using these models: from django.db import models class Author ( models . Model ): ... class Book ( models . Model ): author = models . ForeignKey ( Author , on_delete = models . CASCAD ... E ) ... …and this loop: for book in books : print ( book . author . name ) …where books is a QuerySet of Book instances using some fetch mode. FETCH_ONE Fetches the missing field for the c ... NE for the above example would use: 1 query to fetch books N queries, where N is the number of books, to fetch the missing author field …for a total of 1+N queries. This query pattern is known as the "N+1 queries problem" because it often leads to perf ... ... す。2つの引数の間の trigram 類似度を返します。 使用例: >>> from django.contrib.postgres.search import TrigramSimilarity >>> Author . objects . create ( name = "Katy Stevens" ) >>> Author . objects . create ( name = "Stephen Keats" ) >>> test = "Katie St ... ephens" >>> Author . objects . annotate ( ... similarity = TrigramSimilarity ( "name" , test ), ... ) . filter ( ... similarity__gt = 0.3 ... ... ) . order_by ( "-similarity" ) <QuerySet [<Author: Katy Stevens>, <Author: Stephen Keats>]> TrigramWordSimilarity class TrigramWordSimilarity ( string , expression , ** ext ... ... ストのクエリ Text-based fields have a selection of matching operations. For example, you may wish to allow the lookup of an author like so: >>> Author . objects . filter ( name__contains = "Terry" ) [<Author: Terry Gilliam>, <Author: Terry Jones>] これは ... ないルックアップがの方が使いやすいでしょう。英語以外の名前を扱う場合、さらなる改善は アクセントなし比較 を使うことです: >>> Author . objects . filter ( name__unaccent__icontains = "Helen" ) [<Author: Helen Mirren>, <Author: Helena Bonham Carter>, <Autho ... がヒットしますが、その逆はヒットしません。もう一つの方法は trigram_similar 比較を使うことです。 例えば次のようにします: >>> Author . objects . filter ( name__unaccent__lower__trigram_similar = "Hélène" ) [<Author: Helen Mirren>, <Author: Hélène Joy>] 今 ... ... を使用します。これらのモデルは、一連のオンライン書店の在庫を追跡するために使用されます。 from django.db import models class Author ( models . Model ): name = models . CharField ( max_length = 100 ) age = models . IntegerField () class Publisher ( models ... s . IntegerField () price = models . DecimalField ( max_digits = 10 , decimal_places = 2 ) rating = models . FloatField () authors = models . ManyToManyField ( Author ) publisher = models . ForeignKey ( Publisher , on_delete = models . CASCADE ) pubdat ... 籍の一覧を取得しようとする場合には、それぞれの書籍に寄稿している著者が何名いるのかを知りたいこともあるでしょう。 各 Book は Author に対して多対多の関係を持っています。この関係を QuerySet 内の各書籍について集計できます。 オブジェクトごとの集計は annotate ... ... オブジェクトに対して処理を行いたい場合、 SingleObjectMixin が提供する機能が必要になります。 We'll demonstrate this with the Author model we used in the generic class-based views introduction . views.py from django.http import HttpResponseForbidden , Htt ... t reverse from django.views import View from django.views.generic.detail import SingleObjectMixin from books.models import Author class RecordInterestView ( SingleObjectMixin , View ): """Records the current user's interest in an author.""" model = Aut ... , request , * args , ** kwargs ): if not request . user . is_authenticated : return HttpResponseForbidden () # Look up the author we're interested in. self . object = self . get_object () # Actually record interest somehow here! return HttpResponseRedi ... 2026年10月08日