Just copying my emailed response here for posterity.
>
> I have a database that stores unit numbers in postgresql
eg 3B , 2, 5 , 4C, 1 etc.. so it had to be string type for the
letters
but now i want to order them in ascending order on the template eg.
I would do the following:
In your Unit object, create a new field called num_value.
Add or override the save() method on Unit so that everytime a new Unit
is saved, you compute num_value internally and store it in the DB.
That way you will be able to use the following for ordering purposes:
unit_list = Unit.objects.all().order_by('num_value')
The save method can use regular expressions to grab the numeric value
from your unit field. Something like this:
import re
num_val_regex = re.compile('\d+')
# Put the above two lines somewhere at the top of your models.py
Now Unit.save() could do the following:
def save(self):
matches = num_val_regex.findall(self.unit)
if matches:
self.num_value = matches[0] # The first consecutive numeric
digits
else:
self.num_value = 0
super(Unit, self).save()
If you know that there is only a single character value at the end of
the unit field's value, you can compute the num_value without the
regex:
self.num_value = self.unit[:-1]
Hope this helps,
-Rajesh
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---