Posted By

magicrebirth on 12/22/09


Tagged

model kwargs queryset


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

sapphiriq
pjv


Django kwargs


 / Published in: Django
 

  1. class Entry( models.Model ):
  2. user = models.ForeignKey( User, related_name = 'user_relation' )
  3. category = models.ForeignKey( Category, related_name = 'category_relation' )
  4. title = models.CharField( max_length = 64 )
  5. entry_text = models.TextField()
  6. deleted_datetime = models.DateTimeField()
  7.  
  8.  
  9.  
  10. kwargs = {
  11. # you can set common filter params here
  12. }
  13.  
  14. # will return entries which don't have a deleted_datetime
  15. if exclude_deleted:
  16. kwargs[ 'deleted_datetime__isnull' ] = True
  17.  
  18. # will return entries in a specific category
  19. if category is not None:
  20. kwargs[ 'category' ] = category
  21.  
  22. # will return entries for current user
  23. if current_user_only:
  24. kwargs[ 'user' ] = request.user
  25.  
  26. # will return entries where titles match some search query
  27. if title_search_query != '':
  28. kwargs[ 'title__icontains' ] = title_search_query
  29.  
  30. # apply all filters and fetch entries that match all criteria
  31. entries = Entry.objects.filter( **kwargs )

Report this snippet  

You need to login to post a comment.