django - SimpleListFIlter Default -


i have boolean field on model represents whether has canceled membership or not. trying create custom simplelistfilter allows field filtered on.

however, want show not canceled default. there someway select "no" option default? filter far:

class canceledfilter(simplelistfilter):      title = 'canceled'      # parameter filter used in url query.     parameter_name = 'canceled'      def lookups(self, request, model_admin):         return (             (true, 'yes'),             (false, 'no'),         )      def queryset(self, request, queryset):          if self.value() true or self.value() none:             return queryset.filter(canceled=true)         if self.value() false:             return queryset.filter(canceled=false) 

edit: should have been bit clearer. trying in admin interface. when add above filter list_filter in admin. filter on side of admin page 3 choices: all, yes , no.

i "no" choice or none of choices set default. instead "all" choice set default. there none hacky way set default filter choice or that.

basiclly in admin when view members, want show active (not canceled) default. if click "all" or "yes" want show canceled ones.

update: note same question default filter in django admin, question 6 years old. accepted answer marked requiring django 1.4. not sure if answer still work newer django versions or still best answer.

given age of answers on other question, not sure how should proceed. don't think there way merge two.

had same , stumbled upon question. how fixed in code (adapted example):

class canceledfilter(simplelistfilter):  title = 'canceled'  # parameter filter used in url query. parameter_name = 'canceled'  def lookups(self, request, model_admin):     return (         (2, 'all'),         (1, 'yes'),         (0, 'no'),     )  def queryset(self, request, queryset):      if self.value() none:         self.used_parameters[self.parameter_name] = 0     else:         self.used_parameters[self.parameter_name] = int(self.value())     if self.value() == 2:         return queryset     return queryset.filter(cancelled=self.value()) 

some explanation required. querystring part of url, , name implies: query string. values come in strings, not booleans or integers. when call self.value(), returns string.

if examine url when click on yes/no, when not using custom list filter, you'll see encodes 1/0, not true/false. went same scheme.

for completeness , our future readers, added 2 all. without verifying, assume none before. none used when nothing selected, defaults all. except, in our case needs default false, had pick different value. if don't need option, remove final if-block in queryset method, , first tuple in lookups method.

with out of way, how work? trick in realising self.value() returns:

self.used_parameters.get(self.parameter_name, none) 

which either string, or none, depending on whether key found in dictionary or not. that's central idea: make sure contains integers , not strings, self.value() can used in call queryset.filter(). special treatment value all, 2: in case, return queryset rather filtered queryset. special value none, means there no key parameter_name in dictionary. in case, create 1 value 0, false becomes default value.

note: logic incorrect there; want non-cancelled default, treat none same true. version corrects this.

ps: yes, check 'true' , 'false' rather true , false in querystring method, you'd notice correct selection not highlighted because first elements in tuple don't match (you're comparing strings booleans then). tried making first elements in tuples strings too, i'd have string comparison or eval match 'true' true, kind of ugly/unsafe. best stick integers, in example.


Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -