In one of my project I have the following setup which is used to hold in the same column a value which can be an hostname or an IP address
from django.db.models import CharField, Transform, Model
from django.db.models.functions import Cast
from netfields.fields import InetAddressField
class MultiValueField(CharField):
"""Wrapper on base ``django.db.model.CharField`` to support to-inet transform."""
@MultiValueField.register_lookup
class ToInet(Cast, Transform):
"""Transform to force value to be casted as ``inet``"""
lookup_name = 'to_inet'
def __init__(self, expression):
super().__init__(expression, InetAddressField())
class NetworkProperty(models.Model):
value = MultiValueField(max_length=255)
After updating to 1.4.0 I get the following error when using NetworkProperty.objects.filter(value__to_inet='192.168.1.1'):
AttributeError: 'InetAddressField' object has no attribute 'model'
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
../.venv/lib/python3.13/site-packages/django/db/models/query.py:386: in __iter__
self._fetch_all()
../.venv/lib/python3.13/site-packages/django/db/models/query.py:1954: in _fetch_all
self._result_cache = list(self._iterable_class(self))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
../.venv/lib/python3.13/site-packages/django/db/models/query.py:273: in __iter__
for row in compiler.results_iter(
../.venv/lib/python3.13/site-packages/django/db/models/sql/compiler.py:1572: in results_iter
results = self.execute_sql(
../.venv/lib/python3.13/site-packages/django/db/models/sql/compiler.py:1610: in execute_sql
sql, params = self.as_sql()
^^^^^^^^^^^^^
../.venv/lib/python3.13/site-packages/django/db/models/sql/compiler.py:794: in as_sql
self.compile(self.where) if self.where is not None else ("", [])
^^^^^^^^^^^^^^^^^^^^^^^^
../.venv/lib/python3.13/site-packages/django/db/models/sql/compiler.py:577: in compile
sql, params = node.as_sql(self, self.connection)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
../.venv/lib/python3.13/site-packages/django/db/models/sql/where.py:151: in as_sql
sql, params = compiler.compile(child)
^^^^^^^^^^^^^^^^^^^^^^^
../.venv/lib/python3.13/site-packages/django/db/models/sql/compiler.py:577: in compile
sql, params = node.as_sql(self, self.connection)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
../.venv/lib/python3.13/site-packages/django/db/models/lookups.py:426: in as_sql
return super().as_sql(compiler, connection)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
../.venv/lib/python3.13/site-packages/django/db/models/lookups.py:258: in as_sql
rhs_sql, rhs_params = self.process_rhs(compiler, connection)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
../.venv/lib/python3.13/site-packages/django/db/models/lookups.py:141: in process_rhs
return self.get_db_prep_lookup(value, connection)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
../.venv/lib/python3.13/site-packages/django/db/models/lookups.py:291: in get_db_prep_lookup
else get_db_prep_value(v, connection, prepared=True)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
def get_db_prep_value(self, value, connection, prepared=False):
if value is None:
return None
> if self.model._meta.get_field(self.name).get_internal_type() == 'ArrayField':
^^^^^^^^^^
E AttributeError: 'InetAddressField' object has no attribute 'model'
../.venv/lib/python3.13/site-packages/netfields/fields.py:97: AttributeError
Which is caused by the change in f7529f2 in which was removed the check that self.model was present, because in this case che output_field does not receive any "model".
If I try to set the model from LHS, which is the real model field, it will trigger a FieldDoesNotExist: NetworkProperty has no field named 'None' because neither the name attribute is set in this scenario.
In one of my project I have the following setup which is used to hold in the same column a value which can be an hostname or an IP address
After updating to 1.4.0 I get the following error when using
NetworkProperty.objects.filter(value__to_inet='192.168.1.1'):AttributeError: 'InetAddressField' object has no attribute 'model'Which is caused by the change in f7529f2 in which was removed the check that
self.modelwas present, because in this case cheoutput_fielddoes not receive any "model".If I try to set the model from LHS, which is the real model field, it will trigger a
FieldDoesNotExist: NetworkProperty has no field named 'None'because neither thenameattribute is set in this scenario.