Skip to content
7 changes: 5 additions & 2 deletions ormar/queryset/field_accessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion tests/test_model_definition/test_fields_access.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down