I have a document that is built of sections, subsections, and clauses.
Section 1
Subsections 1.1, 1.2, 1.3
Clauses 1.1.1, 1.1.2... 1.2.1, 1.2.2...
They are models that have a "number" field to hold the identifier, but this field is a CharField because of the double dot syntax of some of the subsections and clauses, eg...
Clause 1.2.1
If I want to sort the subsections, Django basic sorting algorithm will sort:
1.1
1.2
1.3
...
But it becomes problematic when I have more than 9 subsections or clauses in a group because sorting gives me:
1.1
1.10
1.11
1.2
1.3
...
Is there a way to have these ordered correctly in template, like:
1.1
1.2
1.3
1.4
1.5
1.6
1.7
1.8
1.9
1.10
1.11
I could convert them into floats and then sort them - but this wouldn't work for the clauses.
models.py
class Section(models.Model):
number = models.CharField(max_length=2, unique=True)
name = models.CharField(max_length=150, unique=False)
descriptor = HTMLField(blank=True)
def __str__(self):
return u'%s %s' % (self.number, self.name)
...
class Subsection(models.Model):
number = models.CharField(max_length=5, unique=True)
name = models.CharField(max_length=150, unique=False)
descriptor = HTMLField(blank=True)
fundamental = models.BooleanField()
section = models.ForeignKey(Section, on_delete=models.DO_NOTHING)
def __str__(self):
return u'%s %s' % (self.number, self.name)
class Meta:
ordering = ["number"]
....
class Clause(models.Model):
number = models.CharField(max_length=8, unique=True)
requirements = HTMLField()
audit_part = models.CharField(max_length=64, choices=AUDIT_CHOICES)
subsection = models.ForeignKey(Subsection, on_delete=models.DO_NOTHING)
compliance_level = models.CharField(max_length=64, choices=LEVEL_CHOICES, blank=True, null=True)
def __str__(self):
return u'%s' % (self.number)
class Meta:
ordering = ["number"]
...