__isnull on backwards relationships not working 100%

2 views
Skip to first unread message

Matthew Flanagan

unread,
Jul 22, 2006, 5:43:15 AM7/22/06
to django...@googlegroups.com
Hi,

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

Malcolm Tredinnick

unread,
Jul 22, 2006, 5:54:44 AM7/22/06
to django...@googlegroups.com

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.

[1]
http://groups.google.com/group/django-users/browse_frm/thread/98b7aedf8f9d806c/385833fa2b597afe?lnk=gst&rnum=1#385833fa2b597afe

Regards,
Malcolm


Matthew Flanagan

unread,
Jul 22, 2006, 6:43:28 AM7/22/06
to django...@googlegroups.com
Thanks Malcolm. I couldn't find a ticket for this particular
enhancement so I created http://code.djangoproject.com/ticket/2400.
Reply all
Reply to author
Forward
0 new messages