jimfunk / django-postgresql-netfields

Proper INET and CIDR fields for Django running on PostgreSQL
BSD 3-Clause "New" or "Revised" License
152 stars 65 forks source link

Request to support rhs lookup expressions #90

Closed higherorderfunctor closed 5 years ago

higherorderfunctor commented 5 years ago

Would it be possible to support resolving RHS expressions in the custom lookups?

# query to find devices whose IP is not assigned to an interface
(
  Device.objects
  .prefetch_related('interface')
  .filter(
      ~Q(interface__interface_address__net_contains=F('ip_address'))
  )
  .values('name', 'ip_address')
)

They appear to pass through as a literal.

Col(networks_device, networks.Device.ip_address) does not appear to be an IPv4 or IPv6 interface.

Looking at the django source, they appear to have some boilerplate that may work.

    def get_prep_lookup(self):
        if hasattr(self.rhs, 'resolve_expression'):
            return self.rhs
        if self.prepare_rhs and hasattr(self.lhs.output_field, 'get_prep_value'):
            return self.lhs.output_field.get_prep_value(self.rhs)
        return self.rhs

If I get some time at the end of my sprint, I will see if I can work on this, but I wanted to at least get it documented.

Thank you.