Opened 6 years ago

Last modified 6 years ago

#22724 assigned Cleanup/optimization

Improve SingleObjectMixin

Reported by: tomc Owned by: Alex Myasoedov
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 6 years ago by Maxime Turcotte

Cc: maxime.turcotte@… added

comment:2 Changed 6 years ago by Aymeric Augustin

Triage Stage: UnreviewedAccepted

Accepting per mjtamlyn's comment on #21898.

comment:3 Changed 6 years ago by Alex Myasoedov

Has patch: set
Owner: changed from nobody to Alex Myasoedov
Status: newassigned

comment:4 Changed 6 years ago by Tim Graham

Needs documentation: set
Needs tests: set

Needs tests and (probably) documentation.

comment:5 Changed 6 years ago by Danilo Bargen

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 6 years ago by Danilo Bargen

Needs tests: unset

comment:7 Changed 6 years ago by Tim Graham

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 6 years ago by Marc Tamlyn

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 6 years ago by Maxime Turcotte

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