Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
development:python:django:models [2021/01/12 11:24]
kalenpw
development:python:django:models [2021/06/29 15:18] (current)
kalenpw
Line 12: Line 12:
 # filtered by field # filtered by field
 posts = Post.objects.filter(pub_date__year=2020) # can chain additional .filter()  posts = Post.objects.filter(pub_date__year=2020) # can chain additional .filter() 
-posts = posts.filter(pub_date__month=12) 
 # queries are done lazily and won't be ran until necessary # queries are done lazily and won't be ran until necessary
 +posts = posts.filter(pub_date__month=12)
  
 print(posts) # now the query actually runs print(posts) # now the query actually runs
 +</code>
  
 +%%__%% filters for queries
 +<code python>
 +# use a __ (dunder) after a field name to access different forms of it ie
 +posts = Post.objects.filter(title="A New Title")
 +posts = Post.objects.filter(title__contains="New") # can also use icontains for case insensitive
 +posts = Post.objects.filter(title__startswith="A")
 +posts = Post.objects.filter(pub_date__year=2020) # can also use month, day
 </code> </code>
 +
 +----