#7582 closed (fixed)
Sorting by nullable ForeignKey
Reported by: | Owned by: | nobody | |
---|---|---|---|
Component: | contrib.admin | Version: | dev |
Severity: | Keywords: | ||
Cc: | joel@… | Triage Stage: | Accepted |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
In the admin site (using latest newforms-admin), it is not possible to sort a table by a ForeignKey with null=True.
See this little example:
class Group(models.Model): name = models.CharField(max_length=200, primary_key=True) class Person(models.Model): name = models.CharField(max_length=200, primary_key=True) group = models.ForeignKey(Group, blank=True, null=True)
You cannot sort Person by group here.
See also http://groups.google.com/group/django-users/browse_thread/thread/f1f0e50a731834d2/3a44be2ab21e778b
Patch By Karen Tracey <kmtracey@…>
Attachments (1)
Change History (10)
by , 16 years ago
Attachment: | nullorder.diff added |
---|
comment:1 by , 16 years ago
Triage Stage: | Unreviewed → Accepted |
---|
comment:2 by , 16 years ago
Yes, the issue is whether the Admin puts a link in the column header that allows you to sort by the associated field. As mentioned in the list discussion, the restriction has been there since initial import of the code into the current repository. I assume the Admin didn't allow it because the ORM code didn't used to support it. Now it does, I think the Admin restriction can be removed as well. Also mentioned at the tail end of that list discussion is that different DBs may put null values in different places (either first or last), so behavior may not be entirely consistent from DB to DB, but I don't see that as a showstopper for allowing sorting by these fields.
comment:3 by , 16 years ago
milestone: | 1.0 alpha → 1.0 |
---|
This is neither backwards incompatible, nor a feature regression, therefore not needed for the nfa merge(and therefore not 1a).
comment:4 by , 16 years ago
Nevermind on the new patch; I think the problem was on my end (using git apply
instead of patch
).
Also, there doesn't seem to be anything in the admin docs indicating that you can't re-sort on nullable ForeignKey
fields, so it doesn't look like any changes are required there. In fact, I would argue that if the behavior isn't going to be allowed, that should be documented.
comment:5 by , 16 years ago
Cc: | added |
---|
comment:6 by , 16 years ago
milestone: | 1.0 → post-1.0 |
---|---|
Version: | newforms-admin → SVN |
This is sort of a minor feature request, and as such could easily be bundled with a 1.x release later on. Plus, the last patch is almost two months old, which means it almost certainly won't work on current trunk.
comment:7 by , 16 years ago
Myself I'd call it removing remnants of an old bug (prohibiting something that wasn't handled correctly before queryset-refactor), not an enhancement. As jpwatts mentions, there's no documentation explaining why these fields are not made sortable in the admin. Thus the current behavior is confusing to the user, since there is no obvious reason for these fields to be treated differently than all the others that are automatically sortable. (Which is why it came up on the user's list in the first place.)
So I'd argue it should be fixed for 1.0, or documented. Fixing it is easier: there's the patch, which does in fact still apply correctly as of [8472].
comment:8 by , 16 years ago
Resolution: | → fixed |
---|---|
Status: | new → closed |
If I understand correctly, the issue here is that
django.contrib.admin
isn't making the column re-sortable when a nullableForeignKey
appears in thelist_display
of aModelAdmin
class.I've tested the patch and it fixes the above issue by removing the code that explicitly prevents the behavior. It looks to me like that code shouldn't have survived queryset-refactor anyway.
I did notice that the patch as it stands doesn't apply to the top-level directory, so I'll be attaching a version that does, but is otherwise unmodified.