Skip to content

Instantly share code, notes, and snippets.

@icarovirtual
Last active April 21, 2020 21:30
Show Gist options
  • Save icarovirtual/e6d26dc2214ead9d4090e8a747e34bae to your computer and use it in GitHub Desktop.
Save icarovirtual/e6d26dc2214ead9d4090e8a747e34bae to your computer and use it in GitHub Desktop.
class Article(models.Model):
class Status(models.TextChoices):
DRAFT = '0', "Draft"
PUBLISHED = '1', "Published"
REMOVED = '2', "Removed"
status = models.CharField(choices=Status.choices, max_length=1)
class StatusFilter(admin.SimpleListFilter):
title = "Status"
parameter_name = 'status'
def lookups(self, request, model_admin):
return Article.Status.choices
def queryset(self, request, queryset):
value = self.value()
if value:
return queryset.filter(status=value)
return queryset
@admin.register(Article)
class ArticleAdmin(admin.ModelAdmin):
list_filter = [StatusFilter]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment