问题标签 [django-managers]
For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.
django - Django:模型继承自带有自定义管理器的基本模型。经理可以有动态变量吗?
我需要让我的所有模型都继承这个经理(没有测试过这个经理,这可能很荒谬,所以我也完全愿意接受建议/批评):
你可以看到我正在尝试做的事情。根据我正在处理的帐户限制到处过滤的需要。
让这位经理与我的所有模型一起工作的最佳方式是什么?用抽象基模型吗?另外,我如何从视图级别将帐户变量传递给它?这一切都是错误和邪恶的吗?一个星期以来,我一直在努力寻找一种方法来克服这个问题:(。
django - 在不破坏 DRY 的情况下自定义 QuerySet 和 Manager?
我正在尝试找到一种在不破坏 DRY的情况下实现自定义QuerySet
和自定义的方法。Manager
这是我到目前为止所拥有的:
这工作正常,直到我做这样的事情:
这会立即破坏所有内容,因为 与QuerySet
没有相同的方法Manager
。我尝试创建一个自定义QuerySet
类,并在 中实现它MyInquiryManager
,但我最终复制了我所有的方法定义。
我还发现这个片段有效,但我需要将额外的参数传递给for_user
它,因为它严重依赖于重新定义get_query_set
.
有没有办法在不重新定义我在子类QuerySet
和Manager
子类中的所有方法的情况下做到这一点?
django - django访问自定义管理器中的登录用户
我想在我写的自定义管理器中访问当前登录的用户。我想这样做,以便我可以过滤结果以仅显示他们有权访问的对象。
有没有在没有实际传递的情况下这样做?类似于它在可以执行 request.user 的视图中的工作方式。
谢谢
django - django 模型的过滤管理器,由用户自定义
我有一个模型,像这样:
我想覆盖它的默认管理器。但我不知道如何将当前用户变量传递给经理,所以我必须这样做:
我如何通过覆盖经理来摆脱它?
谢谢。
python - Django 模型是否继承管理器?(我的好像没有)
我有 2 个模型:
我希望B.objects
让我访问 的实例ClassAManager
,但事实并非如此......
为什么不从B
继承objects
属性A
?
django - Refactoring a custom User model to user UserProfile: Should I create a custom UserManager or add user.get_profile() dozens of times?
I have been refactoring an app that had customized the standard User model from django.contrib.auth.models by creating a UserProfile and defining it with AUTH_PROFILE_MODULE.
The problem is the attributes in UserProfile are used throughout the project to determine what the User sees.
I had been creating tests and putting in this type of statement repeatedly:
This is tedious and I'm now wondering if I'm violating the DRY principle.
Would it make more sense to create a custom UserManager that automatically loads the UserProfile data when the user is requested.
I could even iterate over the UserProfile attributes and append them to the User model. This would save me having to update all the references to the custom model attributes that litter the code.
Of course, I'd have to reverse to process for to allow the User and UserProfile models to be updated correctly.
Which approach is more Django-esque?
django - Django 经理
我有以下模型代码:
现在通过注册 MusicManager() 和 SportManager() 我可以调用 Event.music.all() 和 Event.sport.all() 查询。但是我怎样才能创建 Event.music.count() ?我应该在 MusicManager 的 count_music() 函数中调用 self.all() 以仅查询具有“音乐”类别的元素,还是我仍然需要先过滤它们以搜索类别?
django - 管理器、模型继承或在 django 中对用户进行切片?
我正在 Django 中编写一个项目,其中有 5 种用户组:
- 第 1 组
- 第 2 组
- ...
然后我有一个模型,与用户有很多关系的项目,项目有一个所有者(Group1 中的一个用户)、一个客户(Group2 中的一个用户)和许多相关用户(Group3 中的用户)。
我想知道哪种是编写这种关系的正确方法。我很想写一些类似的东西:
以某种方式定义了 Owner、Customer 和 RelatedUser 类。
我不知道如何实现这一点。我不想使用模型继承,因为我只想要一个表用户。甚至经理似乎也没有帮助我。实际上我正在使用这样的东西:
主要是因为从空数据库组“客户”开始时不存在并引发错误。
哪种方式是负担得起的?
提前致谢
django - 自定义管理器过滤站点上的对象但不在管理员中?
我按照这个示例进行操作,效果很好,但我想知道是否可以设置一个异常,以便当我在管理员中时所有对象都显示出来(活动和非活动)。这可能很简单,但我在文档中找不到如何操作。
这是我的经理现在的样子:
django - 无法通过模型实例访问管理器
我正在尝试在另一个中获取模型对象实例,并引发此错误:
这是我的模型:
这是我的看法:
我得到: