The Qiita Advent Calendar 2017 is supported by the following companies, organizations, and services.
The Qiita Advent Calendar 2017 is supported by the following companies, organizations, and services.
Django ORM の QuerySet には、select_related および prefetch_related というメソッドがありますが、イマイチ使い勝手がよく分からなかったりします。 公式ドキュメントにはこう書いてありますが、 select_related works by creating an SQL join and including the fields of the related object in the SELECT statement. For this reason, select_related gets the related objects in the same database query. However, to avoid the much larger result set that would result from joining a
djangoでヘテロなリストのprefetch djangoでヘテロなリストのprefetch。できないと思ったら普通に出来たのでわりとびっくりしたので記事にしてみた。 ヘテロなリスト? ここでのヘテロなリストと言うのは以下のようなリストを指している。 xs = [A(), B(), A(), A(), B(), C()] つまるところ1つの型のオブジェクトのリストではなく複数の型が混在したリストのようなものを指している。このようなリストは複数のことなるアイテムを要素として持つタイムラインのようなページを作ろうとした時に必要になる。例えば、このようなものをgeneric foreignkeyを使って以下の様に実装するということがあるかもしれない。 from django.contrib.contenttypes.fields import GenericForeignKey from dj
djangoでの集計は辛いという話 -- ORMは用法・用量を守って正しく使いましょう djangoのORMの機能の不足にぶち当たり辛いという話。別の言い方をすると、ORMは用法・容量守って正しく使いましょうという感じになるかもしれない。 はじめに 以下のような情報を年齢で丸めた値で集計してヒストグラムのようなものを作りたい。 名前 年齢 foo 10 bar 15 boo 20 結果 rank c 1 2 2 1 SQLでは頑張ればどうにかなる 集計をしたい時など何らかの演算の結果で GROUP BY したい時など結構ある。おそらくきっとある。 例えばヒストグラム的なものを作成したい時など。SQLであれば CASEとWHENを書き連ねることを気にしなければどうにかなる。 sqlite> create table person(name string primary key, age in
追記: 使用しているDjangoのバージョンをはじめ、いくつか古くなってきている点があります。DjangoCongress JP 2018で認証に関する発表を行ったのですが、ブログ記事も用意しています。そちらを参照してください nwpct1.hatenablog.com 追記終わり Djangoでユーザ認証をしたかったので調べてみると,DjangoではUserモデルがはじめから用意されているらしい.そこでDjangoの提供する機能をそのまま使ってログイン・ログアウトを実装してみた. さらに調べてみるとpython-social-authというライブラリを使えば、TwitterやFacebook,Google、Github等のアカウントを使ったOAuth認証が簡単に実装できるみたいなのでそれも試しておく. ソースコードはGithubで公開してます. https://github.com/c-
Upon saving me model 'Products' I would like the uploaded image to be named the same as the pk for example 22.png or 34.gif I don't want to change the format of the image just the name. How can this be done? example of my model so far below... image = models.ImageField( upload_to="profiles", height_field="image_height", width_field="image_width", null=True, blank=True, editable=True, help_text="Pr
You cannot create m2m relations from unsaved objects. If you have the pks, try this: sample_object = Sample() sample_object.save() sample_object.users.add(1,2) Update: After reading the saverio's answer, I decided to investigate the issue a bit more in depth. Here are my findings. This was my original suggestion. It works, but isn't optimal. (Note: I'm using Bars and a Foo instead of Users and a S
I have a question about using ugettext and gettext_lazy() for translations. I learned that in models I should use gettext_lazy(), while in views ugettext. But are there any other places, where I should use gettext_lazy() too? What about form definitions? Are there any performance diffrences between them? Edit: And one more thing. Sometimes, instead of gettext_lazy(), gettext_noop() is used. As doc
Table Of Contents Welcome Contents ⚠ Documentation version These are the Git version docs. Docs for 0.4 (PyPI) are here. Help out! To make this documentation even better, we'd love to receive your feedback and suggestions for improvement! Welcome¶ Django MongoDB Engine is a MongoDB backend for Django, the Python Web framework for perfectionists with deadlines. This documentation is split into seve
こんばんは、Python界のひきこもりです。 モバイルアプリケーションのバックエンド開発などでRESTful APIを先ず設計する必要があるといったケースなんかで便利に利用できそうな、既存のウェブアプリケーションフレームワークの仕組みからRESTfulなAPI構成に特化してパッケージングされたサブフレームワークがいろいろ出てきているので調べました。 フレームワークに必要な機能そもそもフレームワークにどんな機能が必要なのかというのをChoosing an API framework for Django から抜粋。 ページネーション(アイテムの先頭から10件が1ページ目→11から20が2ページ目など) データの投稿とバリデーション メタデータの生成(result, count, next みたいな要素をレスポンスに含める為のしくみのことかな?) レスポンスのハンドリング キャッシュ データの
2012/09/24 Pythonコミュニティが開催する「PyCon JP 2012」が、東京の産業技術大学院大学で2012年9月15日から17日まで開催された。PyCon JPは世界中で行われてるPython Conference(PyCon)の日本版で、今回は2回目。「つながるPython」のテーマのもと初の併設イベントも開催され、東京開催にも関わらずヨーロッパ、オーストラリア、台湾など各国のPythonistaが集うなど、前年のほぼ倍の480人を超える参加者が集まり盛り上がった(レポート:タイムインターメディア・飯野卓見) PyCon JP本編ではPython製サービスの事例として写真共有サービス「SnapDish」やNIFTY Cloudに新しく加わったPaaSの「C4SA」のアーキテクチャ紹介、Pythonの非同期IOフレームワーク、「Gevent」と「Tornado」の発表、P
Django REST Framework Django REST framework is a powerful and flexible toolkit for building Web APIs. Some reasons you might want to use REST framework: The Web browsable API is a huge usability win for your developers. Authentication policies including packages for OAuth1a and OAuth2. Serialization that supports both ORM and non-ORM data sources. Customizable all the way down - just use regular f
きのうの深夜に急に思い立ったので色々書いていきます!最近 Django の日本語情報少なすぎると思んで、ちょっとしたことでもこれからブログいっぱい書いていきます。— Yoichi Fujimoto (@wozozo) February 5, 2012 Django 1.3 から Class-based views が入って mixin とかしまくりでめんどくさくなったなーと思ってます。でも使えると便利なことも多いので、とりあえず JSON を返す view を書いてみましょうー。JSONResponseMixin はドキュメントからのコピペです。 https://docs.djangoproject.com/en/1.3/topics/class-based-views/#more-than-just-html from django import http from django.u
Backbone.js and other client-side MV* frameworks are a relatively new paradigm in designing and building rich web applications. Since this transition is still ongoing, most serious server-side frameworks are just now catching up to this trend. Django included. This is not a critique on Django - most sites and apps are still built in the "traditional" way. Backbone and friends are not a one-size-fi
Cross Site Request Forgery protection¶ The CSRF middleware and template tag provides easy-to-use protection against Cross Site Request Forgeries. This type of attack occurs when a malicious website contains a link, a form button or some JavaScript that is intended to perform some action on your website, using the credentials of a logged-in user who visits the malicious site in their browser. A rel
リリース、障害情報などのサービスのお知らせ
最新の人気エントリーの配信
処理を実行中です
j次のブックマーク
k前のブックマーク
lあとで読む
eコメント一覧を開く
oページを開く