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
Fixed #10743 -- Allowed lookups for related fields in ModelAdmin.list_display. #17357
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi! Thank you for picking this up. Overall looks good, but we should support the lookup in related models even if they are null=True
(see error and failing test).
I continued my testing with the latest code, and don't ask me how I ran into this (because I stumbled upon this by chance) but when using the It took me a while to understand what was going on, but this test shows the issue: diff --git a/tests/admin_changelist/tests.py b/tests/admin_changelist/tests.py
index bb73cda87d..13985a02e3 100644
--- a/tests/admin_changelist/tests.py
+++ b/tests/admin_changelist/tests.py
@@ -1642,6 +1642,22 @@ class ChangeListTests(TestCase):
self.assertContains(response, '<td class="field-parent__name">-</td>')
self.assertContains(response, '<td class="field-parent__parent__name">-</td>')
+ def test_list_display_foreign_field_ordering(self):
+ request = self.factory.get("/grandchild/?o=2")
+ request.user = self.superuser
+
+ class GrandChildAdmin(admin.ModelAdmin):
+ list_display = ["name", "parent__name", "parent__parent__name"]
+
+ m = GrandChildAdmin(GrandChild, custom_site)
+ response = m.changelist_view(request)
+ # we should have a decent assert here!
class GetAdminLogTests(TestCase):
def test_custom_user_pk_not_named_id(self): As you can see, this is independent from having any actual entry in the list. The traceback is:
I think this needs to be fixed, at the very least it should not raise an exception. But, this also raises the question of ordering for these newly allowed columns. I think ordering should be supported, specially because the docs says:
@knyghty What do you think? |
@nessita what I think is that this seems hard to do cleanly 🤔 - or am I missing something. I have pushed a very hacky solution, so hacky I didn't bother to write tests for it, rather I'd like to get feedback first if you can see a less silly way of doing this? |
I don't have an answer beforehand, I need to "seat for a while" and do some investigating. I'm happy to do so, likely next week. If, by any chance, you'd find a "less hacky" way of doing it before that, please mention me and push it! |
@nessita I made a marginal improvement and added tests for ordering as we'll need those either way. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The branch is looking great! I added that I would say is a final round of comments after a thorough review. I spent a considerable amount of time testing it, I tried very hard to break it, I got creative but not enough it seems :-)
One thing though, that AFAIK is by design, is that property from related fields are not allowed. Does this match your expectation?
Thank you!
Just IMO, but yes, I think this is consistent with the rest of Django. You can't use properties for filtering, and of course we certainly couldn't use them here for ordering. So I think it's expected. If it's desirable or not is another question but I think we should leave that for another ticket. |
Thanks for the review @nessita, think I've covered all the bases now, hopefully correctly 😄 |
cf8a965
to
5a00ce8
Compare
Hey @knyghty, I know you are busy because of the moving but wanted to check if this PR should be left opened for a little longer, or if we should try to find someone else that adds the necessary Selenium test for the column ordering. Thank you for your work so far! 💯 |
@nessita please keep this open, I plan to either pair on this with one of my Djangonaut Space cohort, or if nobody is interested, do it myself in the coming weeks. |
Thank you, pairing with a Djangonaut sounds like an amazing learning opportunity for them ❤️ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is very close to merging state!
I just added a few nitpicks and a (soft?) request for a few unit tests for the changes in the utils module.
Let me know your thoughts! Thank you.
tests/admin_changelist/tests.py
Outdated
self.assertTrue("Parent C" in result_list_rows[1].text) | ||
self.assertTrue("Grandchild Z" in result_list_rows[1].text) | ||
self.assertTrue("Parent B" in result_list_rows[2].text) | ||
self.assertTrue("Grandchild Y" in result_list_rows[2].text) | ||
self.assertTrue("Parent A" in result_list_rows[3].text) | ||
self.assertTrue("Grandchild X" in result_list_rows[3].text) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not assertIn()
?
217464d
to
846c83d
Compare
846c83d
to
8fc387b
Compare
8fc387b
to
57b835d
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I left small suggestions.
ca6f95a
to
a5bc5df
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, thanks @knyghty and @nmenezes0 !
…n.list_display. Co-authored-by: Alex Garcia <me@alexoteiza.com> Co-authored-by: Natalia <124304+nessita@users.noreply.github.com> Co-authored-by: Nina Menezes <https://github.com/nmenezes0>
…display. Co-authored-by: Natalia <124304+nessita@users.noreply.github.com> Co-authored-by: Nina Menezes <https://github.com/nmenezes0>
a5bc5df
to
9cefdfc
Compare
Following up on #16726
I rebased the PR, changed the wording slightly (still not very sure about it, it's quite a long sentence), used a sentinel, and added a test for when the check fails, which I think addresses everything in the original PR.