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

Way to sort via related objects? #7

Open
rlafuente opened this issue Nov 30, 2009 · 4 comments
Open

Way to sort via related objects? #7

rlafuente opened this issue Nov 30, 2009 · 4 comments

Comments

@rlafuente
Copy link

So far I couldn't find a way to sort through a related object's field.
Quick and dirty example:

class Blog(models.Model):
    name = models.CharField()
    type = models.CharField()

class Entry(models.Model):
    name = models.CharField()
    blog = models.ForeignKey(Blog)

And i have a table for Entries with three headers: Entry name, Blog name and Blog type.
I can sort the table by Entry name and Blog name, but not by Blog type. If i try

{% anchor 'blog.type' %}

It comes up with a TemplateSyntaxError:
Caught an exception while rendering: missing FROM-clause entry for table "blog"
LINE 1: ...ND NOT ("testapp_entry"."id" IN (110))) ORDER BY "blog...

I'm wondering if there's a bug in here or if it's simply not implemented. A
pointer of what code to check would be great, i could try and fix/add it
myself. I'm betting that this can be accomplished by tweaking
templatetags/sorting_tags.py, but i can't discern how it could be done.

By the way, awesome extension! It was such a pleasure doing away with my hardcoded sorting code :-)

@copiesofcopies
Copy link

I ran into this myself, and found that you can do this by using django's double-underscore syntax, so above you would want to use "blog__type".

Hope this helps, 8 months later ;)

@rlafuente
Copy link
Author

It definitely helps -- many thanks! Weirdly enough, i've been working on another part of my app and was now about to rewrite the sorting code because of this apparent shortcoming.

So thanks a ton for the solution -- it's never late! :)

@copiesofcopies
Copy link

Excellent! Glad you were tracking the bug and able to use this! Totally makes my day.

@copiesofcopies
Copy link

Added a foreign key to the example in the wiki so that others can find this as well.

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