使一个相当复杂的 Django 模型方法可以在管理中排序?

发布于 2024-10-11 04:33:21 字数 1055 浏览 2 评论 0原文

我有一个相当复杂的自定义 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]
        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'

目前,当我单击管理界面中的 current_owner 字段时,Django 会给出我

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

是否也需要创建一个 BookManager?如果是这样,我应该使用什么代码?这不是像上一个问题中的示例那样的简单计数,因此我们将不胜感激:)

谢谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

日记撕了你也走了 2024-10-18 04:33:22

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

admin_order_field 的目的是将非字段属性的排序等同于字段属性的排序。

例如,有效值 current_owner.admin_order_field 可以是 idtitlelibrary_id。显然,这些对您的目的来说都没有意义。

一种解决方案是非规范化并始终将 current_owner 存储为 Book 上的模型字段;这可以使用信号自动完成。

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.

请帮我爱他 2024-10-18 04:33:22

你不能这样做。 admin_order_field 必须是一个字段,而不是一个方法 - 它适用于当您有一个返回基础字段的自定义表示形式的方法时,而不是当您进行动态计算来提供值时。 Django 的管理使用 ORM 进行排序,并且不能对自定义方法进行排序。

You can't do this. admin_order_field has to be a field, not a method - it's meant for when you have a method that returns a custom representation of an underlying field, not when you do dynamic calculations to provide the value. Django's admin uses the ORM for sorting, and that can't sort on custom methods.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文