diff --git a/ormar/queryset/field_accessor.py b/ormar/queryset/field_accessor.py index 3b66905c9..e2170f050 100644 --- a/ormar/queryset/field_accessor.py +++ b/ormar/queryset/field_accessor.py @@ -63,9 +63,12 @@ def __getattr__(self, item: str) -> Any: return object.__getattribute__(self, item) # pragma: no cover def _check_field(self) -> None: - if not self._field: + if ( + not self._field + and not self._access_chain in self._source_model.Meta.table.columns.keys() + ): raise AttributeError( - "Cannot filter by Model, you need to provide model name" + "Cannot filter by M2M field, you need to provide model name" ) def _select_operator(self, op: str, other: Any) -> FilterGroup: diff --git a/tests/test_model_definition/test_fields_access.py b/tests/test_model_definition/test_fields_access.py index 538f8258c..d7db1a1e7 100644 --- a/tests/test_model_definition/test_fields_access.py +++ b/tests/test_model_definition/test_fields_access.py @@ -63,7 +63,7 @@ def test_fields_access(): assert curr_field._source_model == PriceList with pytest.raises(AttributeError): - assert Product.category >= 3 + assert Category.price_lists >= 3 @pytest.mark.parametrize(