【问题标题】:Django ManyToMany field not preselecting rows with custom managerDjango ManyToMany 字段未使用自定义管理器预选行
【发布时间】:2011-06-23 22:08:35
【问题描述】:

Django 1.2.5:我有一个带有自定义管理器的模型。数据已正确保存,但未正确检索相关对象。

我的模型是:

  • 问题 -> 与主观统计相关
  • SubjectiveStatistic 将 Statistic 扩展为代理。它有一个自定义管理器,可将结果集限制为仅在“类型”字段与“主观统计”匹配的情况下(类型字段包含对象的类名)。

问题来了:

class Question(models.Model):
    subjective_statistic = models.ManyToManyField(SubjectiveStatistic, null=True, blank=True)

这是主观统计:

class SubjectiveStatistic(Statistic):
    ## Use a custom model manager so that the default object collection is
    # filtered by the object class name.
    objects = RestrictByTypeManager('SubjectiveStatistic')

    ## Override the __init__ method to set the type field
    def __init__(self, *args, **kwargs):
        self.type = self.__class__.__name__
        return super(SubjectiveStatistic, self).__init__(*args, **kwargs)

    class Meta:
        proxy = True

这里是经理:

from django.db import models

## Custom model manager that returns objects filtered so that 'type' == a
# given string.
class RestrictByTypeManager(models.Manager):
    def __init__(self, type='', *args, **kwargs):
        self.type = type
        return super(RestrictByTypeManager, self).__init__(*args, **kwargs)

    def get_query_set(self):
        return super(RestrictByTypeManager, self).get_query_set().filter(type=self.type)

我需要做什么才能正确返回相关对象? question.subjective_statistic.exists() 不返回任何内容,尽管数据库中存在关系。

也许是因为 RestrictByTypeManager 扩展了 Manager 而不是 ManyRelatedManager (但我不能,因为那是一个内部类)或类似的东西?

【问题讨论】:

    标签: django django-models


    【解决方案1】:

    要使用 Question 模型中的自定义管理器,请在自定义管理器的定义中添加 use_for_related_fields = True

    from django.db import models
    
    ## Custom model manager that returns objects filtered so that 'type' == a
    # given string.
    class RestrictByTypeManager(models.Manager):
    
        use_for_related_fields = True
    
        def __init__(self, type='', *args, **kwargs):
            self.type = type
            return super(RestrictByTypeManager, self).__init__(*args, **kwargs)
    
        def get_query_set(self):
            return super(RestrictByTypeManager, self).get_query_set().filter(type=self.type)
    

    这样,RestrictByTypeManager 将用作SubjectiveStatistic 模型的管理器,可以直接访问,也可以像多对多关系一样反向访问。

    更多信息在这里:Controlling automatic Manager types

    【讨论】:

    • 不幸的是,这没有帮助。它的行为与以前相同。
    • question.subjective_statistic.exists() 的 sql 输出是什么?
    • 啊,我不知道我能做到这一点。 sql是: SELECT (1) AS a FROM console_statistic INNER JOIN survey_question_subjective_statistic ON (console_statistic.id = survey_question_subjective_statistic.subjectivestatistic_id) WHERE (console_statistic.@987654333) survey_question_subjective_statistic.question_id = 15 ) 限制 1'。 console_statistic.type 子句为空。
    • 所以看来我需要以某种方式以相反的方向用“SubjectiveStatistic”初始化 RestrictByTypeManager...
    • 我刚刚在构造函数中将 SubjectiveStatistic 设置为默认值,它现在正在工作。谢谢你的帮助。不过我很想知道是否有更好的方法。
    猜你喜欢
    • 1970-01-01
    • 2013-08-04
    • 2011-10-25
    • 2013-02-16
    • 2022-11-04
    • 1970-01-01
    • 2021-05-07
    • 1970-01-01
    • 2015-02-16
    相关资源
    最近更新 更多