LoginSignup
1
0

More than 3 years have passed since last update.

Djan Gogirls Tutorialを復習する(6) 〜DjangoのORMとクエリセット〜

Last updated at Posted at 2019-07-07

注意事項はこちら。
https://qiita.com/K-Kazutaka/items/1a432bda4f723e6757d6
写経用の記事です。

Django shellを起動する
$ python manage.py shell

次のように表示がかわる
(InteractiveConsole)
">>>"

command.line
>>> from blog.models import Post
>>> Post.objects.all()
<QuerySet [<Post: my post title>, <Post: another post title>]>
>>> from django.contrib.auth.models import User
>>> User.objects.all()

さっき登録したユーザー名を確認。

>>> me = User.objects.get(username='ユーザー名')
>>> Post.objects.create(author=me, title='Sample title', text='Test')
>>> Post.objects.all()

さらに投稿を追加

>>> Post.objects.filter(author=me)
>>> Post.objects.filter(title__contains='title')
>>> from django.utils import timezone
>>> post = Post.objects.get(title="Sample title")
>>> post.publish()
>>> Post.objects.filter(published_date__lte=timezone.now())

オブジェクトの並び替え

>>> Post.objects.order_by('created_date')
>>> Post.objects.order_by('-created_date')

クエリセットをつなげる

>>> Post.objects.filter(published_date__lte=timezone.now()).order_by('published_date')
>>> exit()
1
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
1
0