如何为DetailView指定除pk或slug之外的其他内容 [英] How to specify something other than pk or slug for DetailView

查看:161
本文介绍了如何为DetailView指定除pk或slug之外的其他内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道在Django 1.3中使用DetailView时是否可以使用除pk或lug之外的其他东西。

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.

例如,我目前有:

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())

其中name是模型中的字段。无论如何,DetailView是否可以使用它来抓住我想要的对象并将其传递给我的模板?

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?

推荐答案

ug在Django中没有任何特殊意义。它只是用于标识行的字段的名称。如果您的段塞被称为其他名称,例如 name ,只需将 name 指定为 slug_field 视图子类中的属性。

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.

如果您需要更复杂的东西,可以随时覆盖 get_object 在视图类中。

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

这篇关于如何为DetailView指定除pk或slug之外的其他内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆