List_filter in admin

DECOM_CHOICES = (
    ('N', 'No'),
    ('Y', 'Yes'),
)

class Host(models.Model):
    hostname = models.CharField(max_length=36, unique=True)
    decommissioned = models.CharField(max_length=1, choices=DECOM_CHOICES, default='N')
    ip_address = models.IPAddressField()
    def __unicode__(self):
        return self.hostname

class HostAdmin(admin.ModelAdmin):
    fieldsets = [
        ('Host Info', {'fields': ['hostname','decommissioned','ip_address']}),
    list_display = ('hostname', 'ip_address', 'decommissioned')
    list_filter = ('decommissioned')

Now there is some way to 'All'set <off. > disabled mode filter << 21>

+3
source share
3 answers

I do this by modifying the GET data in the request object before passing it to changelist_view(). Not elegant, but it works.

class MyModelAdmin(admin.ModelAdmin):    
    def changelist_view(self, request, extra_context=None):
        if not request.GET.has_key('decommissioned__exact'):
            q = request.GET.copy()
            q['decommissioned__exact'] = 'N'
            request.GET = q
            request.META['QUERY_STRING'] = request.GET.urlencode()
        return super(MyModelAdmin,self).changelist_view(request, extra_context=extra_context)

Note: I have not tried this exact code here, but you should get this idea.

0
source

, Django ticket, , .

- , "" ""?

0

if ('HTTP_REFERER' in request.META) and (request.META ['HTTP_REFERER']. find ('?') == -1) and (not request.GET.has_key ('status__exact')):

Use this save condition instead of the one specified in the above procedure specified by "gerdemb" so that everything can be selected

0
source

Source: https://habr.com/ru/post/1708248/


All Articles