经理的get_queryset函数可以跨关系查询吗?

时间:2015-07-29 17:23:29

标签: django django-models django-managers

我正在尝试使用自定义Manager类来限制从模型访问对象时的查询集。但是我想用相关模型中的字段来限制查询集,比如

class MyManager(models.Manager):
    def get_queryset(self):
        return super(MyManager, self).get_queryset().filter(user__is_active=True)

class MyModel(models.Model):
    objects = MyManager()
    user = models.OneToOneField(settings.AUTH_USER_MODEL)

但是AppRegistry抱怨说,模型尚未加载,我认为这意味着userget_queryset的交叉查询,即使django.contrib.auth应用程序正在此应用程序之前加载。发生了什么事?

编辑:回溯(参见user__groups__name=GROUP_MEMBER所示的行)

Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/home/vagrant/venv/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 338, in execute_from_command_line
    utility.execute()
  File "/home/vagrant/venv/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 312, in execute
    django.setup()
  File "/home/vagrant/venv/local/lib/python2.7/site-packages/django/__init__.py", line 18, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/home/vagrant/venv/local/lib/python2.7/site-packages/django/apps/registry.py", line 108, in populate
    app_config.import_models(all_models)
  File "/home/vagrant/venv/local/lib/python2.7/site-packages/django/apps/config.py", line 198, in import_models
    self.models_module = import_module(models_module_name)
  File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
  File "/vagrant/members_only/base/models.py", line 255, in <module>
    from base.settings import PAGE_NAME_TO_VIEW
  File "/vagrant/members_only/base/settings.py", line 4, in <module>
    from shb.views import *
  File "/vagrant/members_only/shb/views.py", line 10, in <module>
    from shb.forms import *
  File "/vagrant/members_only/shb/forms.py", line 8, in <module>
    class SHBForm(forms.ModelForm):
  File "/home/vagrant/venv/local/lib/python2.7/site-packages/django/forms/models.py", line 285, in __new__
    opts.help_texts, opts.error_messages)
  File "/home/vagrant/venv/local/lib/python2.7/site-packages/django/forms/models.py", line 212, in fields_for_model
    formfield = f.formfield(**kwargs)
  File "/home/vagrant/venv/local/lib/python2.7/site-packages/django/db/models/fields/related.py", line 1970, in formfield
    'queryset': self.rel.to._default_manager.using(db),
  File "/home/vagrant/venv/local/lib/python2.7/site-packages/django/db/models/manager.py", line 127, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/vagrant/members_only/roster/models.py", line 51, in get_queryset
    user__groups__name=GROUP_MEMBER
  File "/home/vagrant/venv/local/lib/python2.7/site-packages/django/db/models/query.py", line 679, in filter
    return self._filter_or_exclude(False, *args, **kwargs)
  File "/home/vagrant/venv/local/lib/python2.7/site-packages/django/db/models/query.py", line 697, in _filter_or_exclude
    clone.query.add_q(Q(*args, **kwargs))
  File "/home/vagrant/venv/local/lib/python2.7/site-packages/django/db/models/sql/query.py", line 1304, in add_q
    clause, require_inner = self._add_q(where_part, self.used_aliases)
  File "/home/vagrant/venv/local/lib/python2.7/site-packages/django/db/models/sql/query.py", line 1332, in _add_q
    allow_joins=allow_joins, split_subq=split_subq,
  File "/home/vagrant/venv/local/lib/python2.7/site-packages/django/db/models/sql/query.py", line 1144, in build_filter
    lookups, parts, reffed_aggregate = self.solve_lookup_type(arg)
  File "/home/vagrant/venv/local/lib/python2.7/site-packages/django/db/models/sql/query.py", line 1030, in solve_lookup_type
    _, field, _, lookup_parts = self.names_to_path(lookup_splitted, self.get_meta())
  File "/home/vagrant/venv/local/lib/python2.7/site-packages/django/db/models/sql/query.py", line 1367, in names_to_path
    if field.is_relation and not field.related_model:
  File "/home/vagrant/venv/local/lib/python2.7/site-packages/django/utils/functional.py", line 60, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/home/vagrant/venv/local/lib/python2.7/site-packages/django/db/models/fields/related.py", line 110, in related_model
    apps.check_models_ready()
  File "/home/vagrant/venv/local/lib/python2.7/site-packages/django/apps/registry.py", line 131, in check_models_ready
    raise AppRegistryNotReady("Models aren't loaded yet.")
django.core.exceptions.AppRegistryNotReady: Models aren't loaded yet.

编辑:SHBForm

class SHBForm(forms.ModelForm):
    class Meta:
        model = SHB
        fields = (
            'director', # foreignkey to RosterEntry
            ...
        )

    def __init__(self, *args, **kwargs):
        super(SHBForm, self).__init__(*args, **kwargs)
        self.fields['director'].queryset = RosterEntry.objects.get_all_active_members()
        ...

0 个答案:

没有答案