Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to specify something other than pk or slug for DetailView

I was wondering if it was possible to use something besides a pk or slug when you are using a DetailView in Django 1.3.

For example, I currently have:

url(r'^mymodel/(?P<pk>\d+)/$', MyDetailView.as_view())

as my url. Say I wanted something like:

url(r'^mymodel/(?P<name>\d+)/$', MyDetailView.as_view())

where name would be a field in the model. Is there anyway to have the DetailView use that to 'grab' the object I want and pass it on to my template?

like image 939
Omar Estrella Avatar asked Apr 25 '11 17:04

Omar Estrella


1 Answers

A slug doesn't have any particular significance in Django. It's just a name for a field that identifies a row. If your slug is called something else, eg name, just specify name as the slug_field attribute in your view subclass.

If you need something more complicated, you can always override get_object in the view class.

like image 178
Daniel Roseman Avatar answered Oct 18 '22 07:10

Daniel Roseman