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

Allow separate accessor for sort field on columns (code attached) #20

Closed
chriscrowe opened this issue Jul 27, 2011 · 7 comments
Closed

Comments

@chriscrowe
Copy link

In my code I use an accessor for a column that points to a function in my model (the function translates a ManyToMany field into a comma-separated string). But when trying to sort on said column, it will fail because the accessor does not resolve to an actual field on my model. So I added an extra kwarg to the Column initializer so you can specify an optional sort_accessor, which instead of pointing to my model function it points to one of the fields:

In columns.py - modified Column and BoundColumn to accept sort_accessor kwarg
http://dpaste.com/579958/

In tables.py - modified _translate_order_by function
http://dpaste.com/579959/

Sorry-- I would submit a pull request but I haven't forked you project (plus the version I'm using is a few months old), and I don't really have time to set all that up right now.

@bradleyayers
Copy link
Collaborator

Hi thanks for the suggestion, but I'm hesitant to add this feature. As it stands I feel like there's already too many configuration options. This functionality can be achieved by using a render_FOO method, e.g.

def render_m2m_field(self, record):
    return record.m2m_pretty_method()

@thibault
Copy link

thibault commented Oct 4, 2011

I think this feature would be really useful. Let's they thay I've got a table displaying a list of products. There's a column displaying the first and last name of the product's buyer. I should be able to sort by last name.

Anyway, this is a recurrent need I meet using this application.

@bradleyayers
Copy link
Collaborator

Thanks for the input @thibault. What's your feeling about the approach using the render_FOO method? Too verbose for such a common pattern?

def render_m2m_field(self, record):
return record.m2m_pretty_method()

If I were to add this I'm leaning toward calling the argument order_by_accessor or just order_by.

@chriscrowe
Copy link
Author

I fell back to using the render_FOO methods after you shot down my idea... But I do think that it's a bit verbose if you've got lots of columns referring to fields on a foreign key object.

@bradleyayers
Copy link
Collaborator

Okay well this isn't an issue that I've come across personally, but since both @skandocious and @thibault have, and contributed to this ticket it means there's probably a bunch of other people that would want this too. I'll look into adding it.

@thibault
Copy link

thibault commented Oct 5, 2011

Hi, and thank you for your fast reply. Since you ask, I think the render_FOO is ok. Verbose, maybe, but do we really need something shorter, I'm not sure.

An order_by accessor, which would allow me to order a queryset by any field, including in a foreign table would be great.

@twoolie
Copy link

twoolie commented Oct 17, 2011

I remember that there used to be a property you could add to instance methods which would tell the admin interface which field to order with respect to when ordering by that generated value. I can't seem to find it in the docs, but maybe someone else can?

thibault pushed a commit to thibault/django-tables2 that referenced this issue Jan 6, 2012
When using an accessor pointing to a model method, this allows you to add
a custom order method in your Ttable class called "order_by_ACCESSOR".

Refs jieter#20
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

4 participants