问题标签 [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.

0 投票
2 回答
989 浏览

django - 如何从现场添加经理

我想要做的是当某个模型使用我的字段时,它会自动将自定义管理器添加到该模型。

据我所知, contibute_to_class 提供这样的功能

问题是,在我的自定义管理器中,我使用 self.model._default_manager 对默认管理器进行查询,但是当我尝试这样做时,django 说 AttributeError: 'NoneType' object has no attribute '_default_manager'

如果我不使用contribute_to_class 并在我的模型类旁边编写自定义管理器,它会按预期工作。可能是什么问题?

0 投票
4 回答
10926 浏览

python - Django Manager Chaining

I was wondering if it was possible (and, if so, how) to chain together multiple managers to produce a query set that is affected by both of the individual managers. I'll explain the specific example that I'm working on:

I have multiple abstract model classes that I use to provide small, specific functionality to other models. Two of these models are a DeleteMixin and a GlobalMixin.

The DeleteMixin is defined as such:

Basically it provides a pseudo-delete (the deleted flag) instead of actually deleting the object.

The GlobalMixin is defined as such:

It allows any object to be defined as either a global object or a private object (such as a public/private blog post).

Both of these have their own managers that affect the queryset that is returned. My DeleteManager filters the queryset to only return results that have the deleted flag set to False, while the GlobalManager filters the queryset to only return results that are marked as global. Here is the declaration for both:

The desired functionality would be to have a model extend both of these abstract models and grant the ability to only return the results that are both non-deleted and global. I ran a test case on a model with 4 instances: one was global and non-deleted, one was global and deleted, one was non-global and non-deleted, and one was non-global and deleted. If I try to get result sets as such: SomeModel.objects.all(), I get instance 1 and 3 (the two non-deleted ones - great!). If I try SomeModel.objects.globals(), I get an error that DeleteManager doesn't have a globals (this is assuming my model declaration is as such: SomeModel(DeleteMixin, GlobalMixin). If I reverse the order, I don't get the error, but it doesn't filter out the deleted ones). If I change GlobalMixin to attach GlobalManager to globals instead of objects (so the new command would be SomeModel.globals.globals()), I get instances 1 and 2 (the two globals), while my intended result would be to only get instance 1 (the global, non-deleted one).

I wasn't sure if anyone had run into any situation similar to this and had come to a result. Either a way to make it work in my current thinking or a re-work that provides the functionality I'm after would be very much appreciated. I know this post has been a little long-winded. If any more explanation is needed, I would be glad to provide it.

Edit:

I have posted the eventual solution I used to this specific problem below. It is based on the link to Simon's custom QuerySetManager.

0 投票
13 回答
83480 浏览

django - 如何在 Django 中使用不同的设置进行单元测试?

是否有任何简单的机制可以覆盖单元测试的 Django 设置?我的一个模型上有一个管理器,它返回特定数量的最新对象。它返回的对象数量由 NUM_LATEST 设置定义。

如果有人要更改设置,这有可能使我的测试失败。如何覆盖设置setUp()并随后恢复它们tearDown()?如果那不可能,有什么方法可以修改方法或模拟设置吗?

编辑:这是我的经理代码:

管理器用于settings.NEWS_LATEST_MAX对查询集进行切片。getattr()仅用于在设置不存在时提供默认值。

0 投票
2 回答
1704 浏览

django - django 管理器的问题

我有以下模型:

还有一个经理

什么时候,我尝试调用类似 UserProfile.obgets.get_active_members()

我得到了

AttributeError:无法通过 UserProfile 实例访问管理器

能否请你帮忙

0 投票
1 回答
571 浏览

django - 如何从我的带注释的 Django 查询中过滤/排除非活动评论?

我正在使用object_list通用视图快速列出一组文章。每篇文章都附有评论。该查询使用注释Count()数来注释评论数,然后order_by()使用注释数。

注释是django.contrib.comments框架的一部分,并通过通用关系附加到模型。我在我的文章模型中添加了一个明确的反向查找:

问题是,这会计算“非活动”评论;有is_public=False或的is_removed=True。如何排除任何不活跃的评论?

0 投票
4 回答
4307 浏览

python - django manager 代码应该在哪里?

这是一个非常简单的 django 模式问题。我的管理器代码通常存在于 models.py 中,但是当 models.py 真的很大时会发生什么?是否有任何其他替代模式可以让您的管理器代码存在于 models.py 中以实现可维护性并避免循环导入?

可能会问一个问题,为什么 models.py 如此庞大,但让我们假设它的大小和实用性广度是合理的。

0 投票
3 回答
15964 浏览

django - Django 自定义管理器 - 如何仅返回登录用户创建的对象?

我想覆盖自定义对象模型管理器以仅返回特定用户创建的对象。管理员用户仍应使用对象模型管理器返回所有对象。

现在我找到了一种可行的方法。他们建议创建您自己的中间件,如下所示:

在自定义管理器中,您可以调用该get_current_user()方法以仅返回特定用户创建的对象。

这是解决这个用例的好方法吗?这行得通吗?或者这就像“用大锤敲碎坚果”?;-)

只需使用:

在每个视图中对我来说都不是很整洁。

编辑不要使用这种中间件方法!!!

使用下面 Jack M. 所述的方法

经过一段时间的测试后,这种方法的表现非常奇怪,并且通过这种方法,您将全局状态与当前请求混合在一起。

使用下面介绍的方法。这真的很容易,无需使用中间件。

在您的模型中创建一个自定义管理器,其函数期望当前用户或任何其他用户作为输入。

另请参阅有关 middelware 方法的讨论。

0 投票
3 回答
1143 浏览

django - Django:如何组织这个大模型/经理/设计混乱?

在我遇到不好的例子之前总结一下,等:我正在尝试制作一个应用程序,我不必在所有模型中编写代码来限制对当前登录帐户的选择(我没有使用身份验证或帐户或登录的内置功能)。

即,我不想做这样的事情:

我的想法是使用以下自定义管理器创建一个Account(models.Model)模型,并使用我的所有模型的多表继承对其进行子类化。不过,这让我感到非常头疼。我还需要account在每个模型上使用外键吗?我可以访问某个模型实例的父类帐户吗?

请忽略任何明显的语法错误。我在这里输入了整个内容。

这是我想到这样做的地方:Django Namespace project

0 投票
3 回答
6977 浏览

django - Django:你如何从管理器内部访问模型的实例?

我正在寻找的魔法是“uncle=model_thats_using_this_manager_instance.uncle”。看来我应该能够以某种方式做到这一点。我知道我可以说self.model获取模型,但是如何获取实例?

0 投票
2 回答
844 浏览

django - Django:在这种情况下使用抽象基础模型是个好主意吗?

如果我有很多模型,并且我想节省时间,不必到处放外键,这是对的吗?或者,我认为这一切都错了吗?