Skip to content

Instantly share code, notes, and snippets.

@phpdude
Last active Dec 9, 2019
Embed
What would you like to do?
from django.contrib.admin import ModelAdmin, register, SimpleListFilter
from django.db.models.functions import Length, StrIndex, Substr, NullIf, Coalesce
from django.db.models import Value as V
from .models import Item
class AlphanumericSignatureFilter(SimpleListFilter):
title = 'Signature (alphanumeric)'
parameter_name = 'signature_alphanumeric'
def lookups(self, request, model_admin):
return (
('signature', 'Signature (alphanumeric)'),
)
def queryset(self, request, queryset):
if self.value() == 'signature':
return queryset.order_by(
Coalesce(Substr('signature', V(0), NullIf(StrIndex('signature', V(' ')), V(0))), 'signature'),
Length('signature'),
'signature'
)
@register(Item)
class Item(ModelAdmin):
list_filter = [AlphanumericSignatureFilter]
from django.db import models
class Item(models.Model):
signature = models.CharField('Signatur', max_length=50)
def __str__(self):
return self.signature
@phpdude
Copy link
Author

phpdude commented Dec 6, 2019

Raw user input data

image

@phpdude
Copy link
Author

phpdude commented Dec 6, 2019

Data sorted by natural key
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment