#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.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/047.0853d45f5cecb09740e14847beb43051%40djangoproject.com.
For more options, visit https://groups.google.com/groups/opt_out.