I have the following simplified models. Then I create a number of IP
addresses and interfaces, with not all IP addresses being related to
an interface.
class IPAddress(models.Model):
address = models.CharField(maxlength=15)
class Admin: pass
class Interface(models.Model):
name = models.CharField(maxlength=15)
ipaddress = models.ForeignKey(IPAddress)
class Admin: pass
Then when I try:
>>> IPAddress.objects.filter(interface__isnull=False)
I get a list of IPAddresses that have related interfaces.
Then if I try:
>>> IPAddress.objects.filter(interface__isnull=True)
I get an empty list when I expected to be getting the IP addresses
that are not related to any interfaces. Right?
Ultimately I'd like to do this:
class Interface(models.Model):
name = models.CharField(maxlength=15)
ipaddress = models.ForeignKey(IPAddress,
limit_choices_to={'interface__isnull':True})
class Admin: pass
so that the admin will only should "free" ip addresses not ones that
ae already used.
Is this a bug? Am I doing the wrong lookup?
regards
matthew
It's not a bug. But it could be argued it's a missing feature. Because
the reverse relation does not exist yet, you cannot query against it. We
would need to special case this type of situation. For the moment, you
will need to construct a custom SQL query if you want to do this (it's
not too painful).
If there's not a ticket for it already (it came up in [1]), maybe file
an enhancement request.
Regards,
Malcolm