Last active
April 21, 2020 21:30
-
-
Save icarovirtual/e6d26dc2214ead9d4090e8a747e34bae to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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