自定义 Django 模型管理器在尝试使用时出错

Custom Django Model Managers giving error when trying to use it

我正在尝试编写一个简单的模型管理器来过滤特定字段。

我有一个看起来像这样的模型:

    """Model definition for Period."""
    year = models.ForeignKey(Year, on_delete=models.PROTECT)
    name = models.CharField(max_length=50)
    active = models.BooleanField(default=False)

    objects = models.Manager()
    active_year_periods = PeriodManager().active_year_periods()

我的经期经理长这样:

class PeriodManager(models.Manager):
    """
    The manager for Period objects
    """

    def active_year_periods(self):
        """
        Gets the Period objects for the active year

        Returns:
            Period<Queryset> -- Periods for an active year
        """
        return super().get_queryset().filter(year__active=True)

我写了一些简单的东西来测试这个特定的管理器,但是 运行 遇到一个错误,我不确定它是什么意思:

  File "/dir/path/myapp/models.py", line 44, in <module>
    class Period(models.Model):
  File "/dir/path/myapp/models.py", line 55, in Period
    active_year_periods = PeriodManager().active_year_periods()
  File "/dir/path/myapp/managers.py", line 19, in active_year_periods
    return self.get_queryset().filter(year__active=True)
  File "/dir/path/venv/lib/python3.7/site-packages/django/db/models/query.py", line 892, in filter
    return self._filter_or_exclude(False, *args, **kwargs)
  File "/dir/path/venv/lib/python3.7/site-packages/django/db/models/query.py", line 910, in _filter_or_exclude
    clone.query.add_q(Q(*args, **kwargs))
  File "/dir/path/venv/lib/python3.7/site-packages/django/db/models/sql/query.py", line 1290, in add_q
    clause, _ = self._add_q(q_object, self.used_aliases)
  File "/dir/path/venv/lib/python3.7/site-packages/django/db/models/sql/query.py", line 1318, in _add_q
    split_subq=split_subq, simple_col=simple_col,
  File "/dir/path/venv/lib/python3.7/site-packages/django/db/models/sql/query.py", line 1190, in build_filter
    lookups, parts, reffed_expression = self.solve_lookup_type(arg)
  File "/dir/path/venv/lib/python3.7/site-packages/django/db/models/sql/query.py", line 1049, in solve_lookup_type
    _, field, _, lookup_parts = self.names_to_path(lookup_splitted, self.get_meta())
  File "/dir/path/venv/lib/python3.7/site-packages/django/db/models/sql/query.py", line 297, in get_meta
    return self.model._meta
AttributeError: 'NoneType' object has no attribute '_meta'

这个错误甚至在我的测试还没有出现之前就已经开始了 运行 - 我的测试目前是为了失败而构建的,但它甚至不会因为那个错误而失败。

这是我的测试:

class TestPeriodManager(TestCase):
    """
    Test cases for the Period Manager
    """
    def setUp(self):
        pass    

    def test_active_year_periods_returns_correct_amount(self):
        """
        Test that the PeriodManager active_year_periods method returns
        appropriate amount of Periods
        """
        # periods_in_active_year = swm.Period.active_year_periods
        # print(periods_in_active_year)
        self.assertTrue(False)

知道为什么我会 运行 关注这个问题吗?我这样做是不是全错了?

我正在尝试使用自定义管理器方法,因此最终我可以在需要时传入参数以进行额外过滤。

您需要将 active_year_periods(您的自定义管理器)分配给管理器 class,而不是方法:

models.py:

objects = models.Manager()
active_year_periods = PeriodManager()