Sort with custom collation that sorts numbers in natural order

This commit is contained in:
Johannes Schriewer 2025-01-06 01:00:12 +01:00
parent a9ce23aea7
commit 4c58206fbc
4 changed files with 50 additions and 6 deletions

View file

@ -0,0 +1,44 @@
# Generated by Django 5.1.4 on 2025-01-05 23:09
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('inventory', '0004_alter_default_ordering'),
]
operations = [
migrations.RunSQL("CREATE COLLATION IF NOT EXISTS numeric (provider = icu, locale = 'en@colNumeric=yes');"),
migrations.AlterField(
model_name='formfactor',
name='description',
field=models.CharField(db_collation='numeric', max_length=4096),
),
migrations.AlterField(
model_name='formfactor',
name='name',
field=models.CharField(db_collation='numeric', max_length=255, unique=True),
),
migrations.AlterField(
model_name='item',
name='description',
field=models.CharField(db_collation='numeric', max_length=4096),
),
migrations.AlterField(
model_name='item',
name='name',
field=models.TextField(db_collation='numeric', max_length=255),
),
migrations.AlterField(
model_name='tag',
name='description',
field=models.CharField(db_collation='numeric', max_length=4096),
),
migrations.AlterField(
model_name='tag',
name='name',
field=models.CharField(db_collation='numeric', max_length=255, unique=True),
),
]

View file

@ -2,8 +2,8 @@ from django.db import models
class FormFactor(models.Model):
name = models.CharField(max_length=255, unique=True)
description = models.CharField(max_length=4096)
name = models.CharField(max_length=255, unique=True, db_collation="numeric")
description = models.CharField(max_length=4096, db_collation="numeric")
icon = models.ImageField(null=True, blank=True)
datasheet = models.FileField(null=True, blank=True)

View file

@ -4,8 +4,8 @@ from .container import CanBeContained
class Item(CanBeContained):
name = models.TextField(max_length=255)
description = models.CharField(max_length=4096)
name = models.TextField(max_length=255, db_collation="numeric")
description = models.CharField(max_length=4096, db_collation="numeric")
size = models.PositiveIntegerField(default=1, help_text="Number of sub-compartments this item takes up")
form_factor = models.ForeignKey('inventory.FormFactor', null=True, blank=True, on_delete=models.PROTECT)
manufacturer = models.ForeignKey('inventory.Manufacturer', null=True, blank=True, on_delete=models.PROTECT)

View file

@ -2,8 +2,8 @@ from django.db import models
class Tag(models.Model):
name = models.CharField(max_length=255, unique=True)
description = models.CharField(max_length=4096)
name = models.CharField(max_length=255, unique=True, db_collation="numeric")
description = models.CharField(max_length=4096, db_collation="numeric")
created_at = models.DateTimeField(auto_now_add=True)
changed_at = models.DateTimeField(auto_now=True)