Hello everyone,

I asked around in #django-dev but nobody had much to say, so I decided to
come here instead.

Currently, developers writing class-based views[0] use SelectObjectMixin[1]
to define which object will be handled by a given single-object view
instance (e.g. DetailView).

If I am understanding things correctly, this mixin has, among other things,
a slug_field class attribute which specifies in which model field django
should look for the value of slug, which is an instance attribute usually
filled with a value from a url pattern argument.

Ideally, users should also be able to define the lookup used by django to
find the slug. The current implementation passes slug_field as kwarg to a
.filter() call[2]. This means that views can be written so that slug_field
actually contain a lookup component (e.g. slug_field = foo_iexact), but
this behavior is, IMHO, not obvious to anyone who hasn't read the source.
The docs currently read:

"The name of the field on the model that contains the slug. By default,
> slug_field is 'slug'."
>

... which actually suggest slug_field *must* match a model field, at least
for those of us who tend to just skim the docs as needed.


I propose we add a "lookup" attribute which defaults to "__exact" (for
backwards-compatibility as well as avoiding surprises) and is combined with
slug_field in the filter call so that users can customize the filter kwarg
in an elegant fashion. Before reading the source code carefully, I had
written my own mixin that just used string formatting (i.e. "%s_%s" %
(slug_field, lookup)") to handle this, and I liked how it looked. I am,
however, more than willing to take a different route should anyone suggest
it.

Another solution would be to keep the current code as is, and just add a
note in the docs regarding the flexibility of slug_field. Given that the
attribute name does say *field*, I'm not too fond of this approach.


Again, I'm willing to hear any feedback on this matter, as well as writing
patches and tests, as I expect them to be quite simple and therefore within
my abilities.



Cheers,

André Terra (airstrike)


[0] https://docs.djangoproject.com/en/dev/ref/class-based-views/
[1]
https://docs.djangoproject.com/en/dev/ref/class-based-views/#django.views.generic.detail.SingleObjectMixin
[2]
https://code.djangoproject.com/browser/django/trunk/django/views/generic/detail.py#L40

-- 
You received this message because you are subscribed to the Google Groups 
"Django developers" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/django-developers?hl=en.

Reply via email to