在Django Admin表单界面中对相关对象进行排序
我希望对使用管理表单编辑对象时显示的相关对象进行排序。例如,我想采用以下对象:
class Person(models.Model):
first_name = models.CharField( ... )
last_name = models.CharField( ... )
hero = models.ForeignKey( 'self', null=True, blank=True )
并使用管理界面编辑名字、姓氏和英雄。我想按姓氏、名字(升序)对下拉列表中显示的对象进行排序。我该怎么做?
上下文
- 我正在使用 Django v1.1。
- 我首先在 django 管理文档 中寻求帮助,但是没有找到解决方案
- 正如您在示例中看到的,外键指向自身,但我希望它与指向不同的模型对象相同。
- 奖励积分还能够过滤相关对象(例如~只允许选择具有相同名字的英雄)
I am looking to sort the related objects that show up when editing an object using the admin form. So for example, I would like to take the following object:
class Person(models.Model):
first_name = models.CharField( ... )
last_name = models.CharField( ... )
hero = models.ForeignKey( 'self', null=True, blank=True )
and edit the first name, last name and hero using the admin interface. I want to sort the objects as they show up in the drop down by last name, first name (ascending). How do I do that?
Context
- I'm using Django v1.1.
- I started by looking for help in the django admin docs, but didn't find the solution
- As you can see in the example, the foreign key is pointing to itself, but I expect it would be the same as pointing to a different model object.
- Bonus points for being able to filter the related objects, too (eg~ only allow selecting a hero with the same first name)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
http://docs.djangoproject.com/en/dev/ref /models/options/#ordering
注意:根据 SVN 文档,管理员仅使用第一个排序选项,因此这是最接近您想要的。
http://docs.djangoproject.com/en/dev/ref/models/options/#ordering
Note: According to the SVN docs, the admin only uses the first of the ordering options, so this is the closest you're gettig to what you want.