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

时间:2020-01-09 18:20:15

标签: python django django-models django-managers

我正在尝试编写一个简单的模型管理器,该模型管理器将对特定字段进行过滤。

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

    """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)

为什么我可能遇到这个问题?我要把这都弄错了吗?

我正在尝试使用自定义管理器方法,因此最终我可以在需要的地方传递参数以进行其他过滤。

1 个答案:

答案 0 :(得分:1)

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

models.py

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