Opened 2 years ago

Last modified 22 months ago

#22724 assigned Cleanup/optimization

Improve SingleObjectMixin

Reported by: tomc Owned by: msoedov
Component: Generic views Version: master
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: yes
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


Belatedly following on from

I feel that slug_url_kwarg and pk_url_kwarg don't feel very in keeping with the rest of django to me, and some thought could be put into improving this part of the API.

My personal opinion is that I don't think that SingleObjectMixin should require a pk, slug, or these variables to be set if you provide a queryset of your own. I don't see any reason why we shouldn't have a DetailView without any arguments in the URL at all, if we provide a single object in some manner. At the moment to do this, we have to override get_object() and essentially rewrite it, which does not feel very elegant to me.

Change History (9)

comment:1 Changed 2 years ago by maxocub

  • Cc maxime.turcotte@… added
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

comment:2 Changed 2 years ago by aaugustin

  • Triage Stage changed from Unreviewed to Accepted

Accepting per mjtamlyn's comment on #21898.

comment:3 Changed 2 years ago by msoedov

  • Has patch set
  • Owner changed from nobody to msoedov
  • Status changed from new to assigned

comment:4 Changed 2 years ago by timo

  • Needs documentation set
  • Needs tests set

Needs tests and (probably) documentation.

comment:5 Changed 2 years ago by dbrgn

Tests have been provided by msoedov.

I'm unsure if there's any documentation that needs to be updated... Couldn't find any at a first quick glance, but I could have missed something.

comment:6 Changed 2 years ago by dbrgn

  • Needs tests unset

comment:7 Changed 2 years ago by timo

The documentation to be updated is located here. Be sure to annotate the changes with .. versionchanged:: 1.8 and also mention it in the release notes. It would also be helpful to describe this better than "Improve SingleObjectMixin." in the ticket & commit message.

comment:8 Changed 22 months ago by mjtamlyn

Proposed patch was significantly backwards incompatible (see my comments on the PR). It however should be possible to refactor get_object itself to make a better subclassing experience, possibly by adding an inner get_from_queryset method, or by changing the behaviour when no slug or pk is provided.

comment:9 Changed 22 months ago by maxocub

  • Cc maxime.turcotte@… removed
Note: See TracTickets for help on using tickets.
Back to Top