lambdamusic
2/7/2013 - 9:25 PM

Django: Django kwargs

Django: Django kwargs

class Entry( models.Model ):
    user = models.ForeignKey( User, related_name = 'user_relation' )
    category = models.ForeignKey( Category, related_name = 'category_relation' )
    title = models.CharField( max_length = 64 )
    entry_text = models.TextField()
    deleted_datetime = models.DateTimeField()



kwargs = {
    # you can set common filter params here
}

# will return entries which don't have a deleted_datetime
if exclude_deleted:
    kwargs[ 'deleted_datetime__isnull' ] = True

# will return entries in a specific category
if category is not None:
    kwargs[ 'category' ] = category

# will return entries for current user
if current_user_only:
    kwargs[ 'user' ] = request.user

# will return entries where titles match some search query
if title_search_query != '':
    kwargs[ 'title__icontains' ] = title_search_query

# apply all filters and fetch entries that match all criteria
entries = Entry.objects.filter( **kwargs )