Django:有什么方法可以在管理界面中对模型方法进行排序吗?
我的模型如下:
class Book(models.Model):
title = models.CharField(max_length=400)
def transactions_all_time(self):
latest_transactions = Transaction.objects.filter(book=self, transaction_type=0)
return len(latest_transactions)
class Transaction(models.Model):
book = models.ForeignKey(Book)
user = models.ForeignKey(User)
transaction_type = models.IntegerField(choices=TRANSACTION_TYPES)
我真的很想找到一种方法在管理界面中按标题和 transactions_all_time
列出图书表格,并使该表格可在 transactions_all_time
上排序code> 字段(或者简单地说是相关交易的数量,如果这样更容易的话)。
我知道没有一种简单的方法可以做到这 - 有人能建议一种可能的方法吗?我可以编写某种自定义管理视图吗?
感谢您的帮助。
I have models as follows:
class Book(models.Model):
title = models.CharField(max_length=400)
def transactions_all_time(self):
latest_transactions = Transaction.objects.filter(book=self, transaction_type=0)
return len(latest_transactions)
class Transaction(models.Model):
book = models.ForeignKey(Book)
user = models.ForeignKey(User)
transaction_type = models.IntegerField(choices=TRANSACTION_TYPES)
I'd really like to find a way to list a table of books by title and transactions_all_time
in the admin interface, and make this table sortable on the transactions_all_time
field (or simply the number of related transactions, if that's easier).
I know that there isn't a straightforward way to do this - could anyone suggest a possible approach? Could I write some kind of custom admin view?
Thanks for your help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,您不应该在查询集上调用
len()
来获取项目数,因为它不会转换为 sql 的COUNT
而是获取数据库的所有项目。更好地使用 queryset 的count()
方法:latest_transactions = Transaction.objects.filter(book=self, transaction_type=0).count()
按
ForeignKey
的计数排序应该可以使用类似的东西:有关如何向 django 管理添加可排序列的说明 查看这篇文章!
First of all you shouldn't call
len()
on the queryset to get the number of items as it will not translate to sql'sCOUNT
but fetch all items of the database instead. Better use the queryset'scount()
method:latest_transactions = Transaction.objects.filter(book=self, transaction_type=0).count()
Sorting by a
ForeignKey
's count should be possible with something like that:For instructions on how to add a sortable column to django's admin see this post!
管理员使用的查询集通过 queryset() 方法公开。我的回答或 lazerscience 都不涉及您的交易类型。所以我回到我的例子并说:“这些只是这些电影试图赢得的奖项。如果我只想要电影实际赢得的那些奖项怎么办?”这解决了仅按特定类型的交易进行计数的问题。如果您的 SQL Server 在内部支持 count() 方法(最好!),答案是:
此外,如果您想在列表中显示 Award_count,请注意,由于它不是模型的本机字段,因此它不会当管理员构建其列表构建器工厂时可用。您可以通过将以下内容添加到 ModelAdmin 类来解决此问题:
列表构建器工厂将看到有一个用于生成奖励计数的方法,并且该方法将有权访问由查询集生成的行。维奥拉,给你。
The queryset used by an admin is exposed through the method queryset(). Neither my answer or lazerscience's deals with your transaction type. So I went back to my example and said, "Those are only the awards those films tried to win. What if I only wanted those awards the films actually won?" That addresses your issue of counting only by those transactions of a certain type. Provided your SQL server supports the count() method internally (it had better!) the answer is:
Additionally, if you want to display the award_count in your list, be aware that since it's not a native field of the model it won't be available when the admin builds its list builder factory. You can work around this by adding the following to your ModelAdmin class:
The list builder factory will see that there's a method for generating the award count, and that method will have access to the rows generated by your queryset. Viola', there you go.