Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

helpers.format makes sorting work as if column is text rather than numeric #66

Closed
bmatzelle opened this issue Aug 27, 2014 · 2 comments
Closed

Comments

@bmatzelle
Copy link

I am using the helpers.format function to return numbers with commas included, helpers.format("{0:,}") to be specific, but I'm having an issue because if I click the column to sort the results they are getting returned as if the column were a text (non-numeric) field. Below is the order that the results are getting returned. Here is a screenshot of the table results.

  • 7,786
  • 52
  • 5,406
  • 43
  • 4,692
  • 4
  • 318
  • 2,857
  • 158
  • 1,676

What can I do to have them be returned in the correct order (7,786 > 5,406 > 4692, and so on)?

My development environment is the following:

  • Django 1.6.5
  • Python 2.7.5
  • django-datatable-view 0.7.1
@tiliv
Copy link
Contributor

tiliv commented Aug 28, 2014

Hm... does the middle argument of the column definition correctly reflect a database value, or is it computed via a method?

It sounds like the code is deciding it's not a real database field, and it's manually sorting by post-processed version of the data.

To prevent this, make sure the value's model field is correctly specified as part of the column definition:

datatable_options = {
    'columns': [ ('My Field', 'path__field', helpers.callback("{0:,}")) ],
}

If the field is calculated and not actually available at the query level, you can specify a model method/property name, and then you'll have to return a 2-tuple of data from your datatable callback so that it knows what is "formatted" and what is "raw":

datatable_options = {
    'columns': [ ('My Field', 'path__field', helpers.callback("{0:,}")) ],
}
def get_column_My_Field_data(self, obj, *args, **kwargs):
    text = "{0:,}".format(obj.path.field)
    return (text, obj.path.field)

I think this managed to escape full documentation. I'll see what I can do to add that in. It's not the kind of thing you want to be forced into doing often, but let me know if it changes anything.

@bmatzelle
Copy link
Author

Hm... does the middle argument of the column definition correctly reflect a database value, or is it computed via a method?

It's computed by a method.

If the field is calculated and not actually available at the query level, you can specify a model method/property name, and then you'll have to return a 2-tuple of data from your datatable callback so that it knows what is "formatted" and what is "raw"

Excellent! Thanks for the help!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants