gpt4 book ai didi

python - 如何将 Django 模型元选项应用于我未编写的模型?

转载 作者:太空狗 更新时间:2023-10-30 00:23:51 26 4
gpt4 key购买 nike

我想申请 "ordering"来自 django.contrib.auth.models 的 Django 模型用户的元选项。通常我只是将 Meta 类放在模型的定义中,但在这种情况下我没有定义模型。那么修改User模型的Meta类放在哪里呢?

最佳答案

This is how the Django manual recommends you do it :

You could also use a proxy model to define a different default ordering on a model. The standard User model has no ordering defined on it (intentionally; sorting is expensive and we don't want to do it all the time when we fetch users). You might want to regularly order by the username attribute when you use the proxy. This is easy:

class OrderedUser(User):
class Meta:
ordering = ["username"]
proxy = True

Now normal User queries will be unorderd and OrderedUser queries will be ordered by username.

请注意,要使其正常工作,您需要对 Django 进行主干检查,因为它是相当新的。

如果您无权访问它,则需要摆脱代理部分并以这种方式实现它,这会变得很麻烦。 Check out this article on how to accomplish this.

关于python - 如何将 Django 模型元选项应用于我未编写的模型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/720083/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com