Django:强制选择相关?

发布于 2024-10-16 02:31:23 字数 158 浏览 4 评论 0原文

我已经创建了一个模型,并且正在为其渲染默认/未修改的模型表单。仅此一项就生成了 64 个 SQL 查询,因为它有相当多的外键,而这些查询又具有更多的外键。

每次返回这些模型之一时,是否可以强制它始终(默认情况下)执行select_lated

I've created a model, and I'm rendering the default/unmodified model form for it. This alone generates 64 SQL queries because it has quite a few foreign keys, and those in turn have more foreign keys.

Is it possible to force it to always (by default) perform a select_related every time one of these models are returned?

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

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

发布评论

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

评论(3

十二 2024-10-23 02:31:23

您可以创建一个自定义管理器,然后只需覆盖 get_queryset 即可将其应用到任何地方。例如:(

class MyManager(models.Manager):
    def get_queryset(self):
        return super(MyManager, self).get_queryset().select_related('foo', 'bar')

在 Django 1.6 之前,它是 get_query_set)。

You can create a custom manager, and simply override get_queryset for it to apply everywhere. For example:

class MyManager(models.Manager):
    def get_queryset(self):
        return super(MyManager, self).get_queryset().select_related('foo', 'bar')

(Prior to Django 1.6, it was get_query_set).

毅然前行 2024-10-23 02:31:23

这还有一个有趣的技巧:

class DefaultSelectOrPrefetchManager(models.Manager):
    def __init__(self, *args, **kwargs):
        self._select_related = kwargs.pop('select_related', None)
        self._prefetch_related = kwargs.pop('prefetch_related', None)

        super(DefaultSelectOrPrefetchManager, self).__init__(*args, **kwargs)

    def get_queryset(self, *args, **kwargs):
        qs = super(DefaultSelectOrPrefetchManager, self).get_queryset(*args, **kwargs)

        if self._select_related:
            qs = qs.select_related(*self._select_related)
        if self._prefetch_related:
            qs = qs.prefetch_related(*self._prefetch_related)

        return qs


class Sandwich(models.Model):
    bread = models.ForeignKey(Bread)
    extras = models.ManyToManyField(Extra)

    # ...

    objects = DefaultSelectOrPrefetchManager(select_related=('bread',), prefetch_related=('extras',))

然后您可以在模型类之间轻松地重复使用管理器。作为示例用例,如果您在模型上有一个 __unicode__ 方法,该方法呈现一个包含来自相关模型的一些信息的字符串(或任何其他意味着相关模型几乎< em>总是需要)。

...如果您真的想要变得古怪,这里有一个更通用的版本。它允许您使用 argskwargs 的任意组合在默认查询集上调用任何方法序列。代码中可能存在一些错误,但您明白了。

from django.db import models


class MethodCalls(object):
    """
    A mock object which logs chained method calls.
    """
    def __init__(self):
        self._calls = []

    def __getattr__(self, name):
        c = Call(self, name)
        self._calls.append(c)
        return c

    def __iter__(self):
        for c in self._calls:
            yield tuple(c)


class Call(object):
    """
    Used by `MethodCalls` objects internally to represent chained method calls.
    """
    def __init__(self, calls_obj, method_name):
        self._calls = calls_obj
        self.method_name = method_name

    def __call__(self, *method_args, **method_kwargs):
        self.method_args = method_args
        self.method_kwargs = method_kwargs

        return self._calls

    def __iter__(self):
        yield self.method_name
        yield self.method_args
        yield self.method_kwargs


class DefaultQuerysetMethodCallsManager(models.Manager):
    """
    A model manager class which allows specification of a sequence of
    method calls to be applied by default to base querysets.
    `DefaultQuerysetMethodCallsManager` instances expose a property
    `default_queryset_method_calls` to which chained method calls can be
    applied to indicate which methods should be called on base querysets.
    """
    def __init__(self, *args, **kwargs):
        self.default_queryset_method_calls = MethodCalls()

        super(DefaultQuerysetMethodCallsManager, self).__init__(*args, **kwargs)

    def get_queryset(self, *args, **kwargs):
        qs = super(DefaultQuerysetMethodCallsManager, self).get_queryset(*args, **kwargs)

        for method_name, method_args, method_kwargs in self.default_queryset_method_calls:
            qs = getattr(qs, method_name)(*method_args, **method_kwargs)

        return qs


class Sandwich(models.Model):
    bread = models.ForeignKey(Bread)
    extras = models.ManyToManyField(Extra)

    # Other field definitions...

    objects = DefaultQuerysetMethodCallsManager()
    objects.default_queryset_method_calls.filter(
        bread__type='wheat',
    ).select_related(
        'bread',
    ).prefetch_related(
        'extras',
    )

受 python-mock 启发的 MethodCalls 对象是使 API 更加自然的尝试。有些人可能会觉得这有点令人困惑。如果是这样,您可以将该代码替换为仅接受方法调用信息元组的 __init__ arg 或 kwarg。

Here's also a fun trick:

class DefaultSelectOrPrefetchManager(models.Manager):
    def __init__(self, *args, **kwargs):
        self._select_related = kwargs.pop('select_related', None)
        self._prefetch_related = kwargs.pop('prefetch_related', None)

        super(DefaultSelectOrPrefetchManager, self).__init__(*args, **kwargs)

    def get_queryset(self, *args, **kwargs):
        qs = super(DefaultSelectOrPrefetchManager, self).get_queryset(*args, **kwargs)

        if self._select_related:
            qs = qs.select_related(*self._select_related)
        if self._prefetch_related:
            qs = qs.prefetch_related(*self._prefetch_related)

        return qs


class Sandwich(models.Model):
    bread = models.ForeignKey(Bread)
    extras = models.ManyToManyField(Extra)

    # ...

    objects = DefaultSelectOrPrefetchManager(select_related=('bread',), prefetch_related=('extras',))

Then you can re-use the manager easily between model classes. As an example use case, this would be appropriate if you had a __unicode__ method on the model which rendered a string that included some information from a related model (or anything else that meant a related model was almost always required).

...and if you really want to get wacky, here's a more generalized version. It allows you to call any sequence of methods on the default queryset with any combination of args or kwargs. There might be some errors in the code, but you get the idea.

from django.db import models


class MethodCalls(object):
    """
    A mock object which logs chained method calls.
    """
    def __init__(self):
        self._calls = []

    def __getattr__(self, name):
        c = Call(self, name)
        self._calls.append(c)
        return c

    def __iter__(self):
        for c in self._calls:
            yield tuple(c)


class Call(object):
    """
    Used by `MethodCalls` objects internally to represent chained method calls.
    """
    def __init__(self, calls_obj, method_name):
        self._calls = calls_obj
        self.method_name = method_name

    def __call__(self, *method_args, **method_kwargs):
        self.method_args = method_args
        self.method_kwargs = method_kwargs

        return self._calls

    def __iter__(self):
        yield self.method_name
        yield self.method_args
        yield self.method_kwargs


class DefaultQuerysetMethodCallsManager(models.Manager):
    """
    A model manager class which allows specification of a sequence of
    method calls to be applied by default to base querysets.
    `DefaultQuerysetMethodCallsManager` instances expose a property
    `default_queryset_method_calls` to which chained method calls can be
    applied to indicate which methods should be called on base querysets.
    """
    def __init__(self, *args, **kwargs):
        self.default_queryset_method_calls = MethodCalls()

        super(DefaultQuerysetMethodCallsManager, self).__init__(*args, **kwargs)

    def get_queryset(self, *args, **kwargs):
        qs = super(DefaultQuerysetMethodCallsManager, self).get_queryset(*args, **kwargs)

        for method_name, method_args, method_kwargs in self.default_queryset_method_calls:
            qs = getattr(qs, method_name)(*method_args, **method_kwargs)

        return qs


class Sandwich(models.Model):
    bread = models.ForeignKey(Bread)
    extras = models.ManyToManyField(Extra)

    # Other field definitions...

    objects = DefaultQuerysetMethodCallsManager()
    objects.default_queryset_method_calls.filter(
        bread__type='wheat',
    ).select_related(
        'bread',
    ).prefetch_related(
        'extras',
    )

The python-mock-inspired MethodCalls object is an attempt at making the API more natural. Some might find that a bit confusing. If so, you could sub out that code for an __init__ arg or kwarg that just accepts a tuple of method call information.

挽梦忆笙歌 2024-10-23 02:31:23

创建自定义 models.Manager 并覆盖所有方法(filterget 等)并将 select_lated 附加到每个查询上。然后将此管理器设置为模型上的 objects 属性。

我建议只检查您的代码并在需要的地方添加 select_lated ,因为对所有内容执行 select_lated 都会导致一些严重的性能问题(并且不完全清楚它会发生在哪里)从)。

Create a custom models.Manager and override all the methods (filter, get etc.) and append select_related onto every query. Then set this manager as the objects attribute on the model.

I would recommend just going through your code and adding the select_related where needed, because doing select_related on everything is going to cause some serious performance issues down the line (and it wouldn't be entirely clear where it's coming from).

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