在管理员中排列相当复杂的Django模型方法? [英] Making a fairly complex Django model method sortable in admin?

查看:139
本文介绍了在管理员中排列相当复杂的Django模型方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个相当复杂的自定义Django模型方法。它在管理界面中可见,我现在也想在管理界面中进行排序。



我根据建议添加了 admin_order_field ,但是不完全明白我还需要做什么。

  class Book(models.Model):
id = models.IntegerField(primary_key = True)
title = models.CharField(max_length = 200)
library_id = models.CharField(max_length = 200,unique = True)
def current_owner(self):
latest_transaction = Transaction.objects.filter book = self)[:1]
如果latest_transaction:
如果latest_transaction [0] .transaction_type == 0:
返回latest_transaction [0] .user.windows_id
返回无
current_owner.admin_order_field ='current_owner'

目前,当我点击管理界面,Django给我

  / admin / books / book / 
中的FieldError无法将关键字'current_owner'解析成字段

我还需要做一个BookManager吗?如果是这样,我应该使用什么代码?这不是一个简单的Count,就像上一个问题中的例子,所以帮助不胜感激:)



谢谢!

解决方案

Django管理员不会通过方法或不是模型字段的任何其他属性(即数据库列)的结果对模型进行排序。订单必须在数据库查询中完成,以保持简单有效。



admin_order_field 的目的是将非字段属性的顺序等同于一个字段的顺序。



例如,有效的值 current_owner.admin_order_field 可以是 id title library_id 。显然这些都不适合你的目的。



一个解决方案是去正规化,并将 current_owner 存储为上的模型字段;这可以使用信号自动完成。


I have a reasonably complex custom Django model method. It's visible in the admin interface, and I would now like to make it sortable in the admin interface too.

I've added admin_order_field as recommended in this previous question, but I don't fully understand what else I need to do.

class Book(models.Model):
    id = models.IntegerField(primary_key=True)
    title = models.CharField(max_length=200)
    library_id = models.CharField(max_length=200, unique=True)
    def current_owner(self):
        latest_transaction = Transaction.objects.filter(book=self)[:1]
        if latest_transaction:
            if latest_transaction[0].transaction_type==0:
                return latest_transaction[0].user.windows_id
        return None
    current_owner.admin_order_field = 'current_owner'

Currently, when I click on the current_owner field in the admin interface, Django gives me

FieldError at /admin/books/book/
Cannot resolve keyword 'current_owner' into field

Do I need to make a BookManager too? If so, what code should I use? This isn't a simple Count like the example in the previous question, so help would be appreciated :)

Thanks!

解决方案

The Django admin won't order models by the result of a method or any other property that isn't a model field (i.e. a database column). The ordering must be done in the database query, to keep things simple and efficient.

The purpose of admin_order_field is to equate the ordering of a non-field property to the ordering of something that is a field.

For example, a valid values current_owner.admin_order_field could be id, title or library_id. Obviously none of these makes sense for your purpose.

One solution would be to denormalise and always store current_owner as a model field on Book; this could be done automatically using a signal.

这篇关于在管理员中排列相当复杂的Django模型方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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