[Django] #21898: SingleObjectMixin should not require slug or pk if queryset is given

9 views
Skip to first unread message

Django

unread,
Jan 29, 2014, 5:54:07 AM1/29/14
to django-...@googlegroups.com
#21898: SingleObjectMixin should not require slug or pk if queryset is given
--------------------------------------+--------------------
Reporter: tomc | Owner: nobody
Type: Cleanup/optimization | Status: new
Component: Generic views | Version: master
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
--------------------------------------+--------------------
I might be missing an obvious design decision made here, but it seems to
me that if you give a queryset or define get_queryset() in a class with
the SingleObjectMixin, that I shouldn't need to override get_object() and
essentially dump the same code in but without checking for slug or pk.

Simple use case: I'm rolling my own flatpages style app, different enough
that I don't want to inherit from it. I'm not using the url as the pk as
if someone typos the url the first time, they end up with two pages if
they rename it. So my code ends up looking like this:


{{{
class PageDetail(DetailView):
def get_queryset(self):
return Page.objects.filter(url=self.kwargs.get('url'))

def get_object(self, queryset=None):
if queryset is None:
queryset = self.get_queryset()

try:
obj = queryset.get()
except ObjectDoesNotExist:
raise Http404('No %(verbose_name)s found matching the query' %
{'verbose_name':
queryset.model._meta.verbose_name})

return obj

def get_template_names(self):
return [self.object.template_name]
}}}

I feel like I should be able to just set the queryset without overriding
get_object()

Another option instead of removing the pk or slug check, might be to have
a variable that sets the lookup field that can be anything, not just slug
or pk, and if this isn't set, it reverts to the current behaviour

I should be able to provide a patch for either case if necessary.

--
Ticket URL: <https://code.djangoproject.com/ticket/21898>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Feb 9, 2014, 7:39:56 AM2/9/14
to django-...@googlegroups.com
#21898: SingleObjectMixin should not require slug or pk if queryset is given
-------------------------------------+-------------------------------------
Reporter: tomc | Owner: nobody
Type: | Status: closed
Cleanup/optimization | Version: master
Component: Generic views | Resolution:
Severity: Normal | worksforme
Keywords: | Triage Stage:
Has patch: 0 | Unreviewed
Needs tests: 0 | Needs documentation: 0
Easy pickings: 0 | Patch needs improvement: 0
| UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by mjtamlyn):

* status: new => closed
* needs_better_patch: => 0
* resolution: => worksforme
* needs_tests: => 0
* needs_docs: => 0


Comment:

I'm going to close this ticket, despite the fact I agree the API could be
better. That said for your specific use case there are two reasonable
options with the current API:

Firstly you can specify `slug_url_kwarg = 'url` and `slug_field = 'url'`
and you will actually get the code you want without overriding any
methods. There are some more complex use cases where this works, but I
think in those situations it might be best to just override
`get_queryset`.

There's an argument that we should provide a better set of tools than
`slug_url_kwarg`/`pk_url_kwarg` etc for customising `SingleObjectMixin`,
but that would be a separate ticket.

--
Ticket URL: <https://code.djangoproject.com/ticket/21898#comment:1>

Reply all
Reply to author
Forward
0 new messages