Re: Re: Select all instances of a model that have a null ForeignKey value

2006-09-09 Thread James Bennett

On 9/9/06, cyberco <[EMAIL PROTECTED]> wrote:
> Thank you! That is a pretty obscure method.

It's obscure but it's necessary; the way Django parses the lookup
parameters will drop any parameter of the form 'somefield=None', so
that the parameter never makes it into the eventual database query
(have a look at the function 'parse_lookup' in
django/db/models/query.py to see code which does this -- it's line 710
in that file in the current trunk).

I don't know exactly, but I'd imagine that the reason for this is that
testing for null values is a special operation in SQL -- in your
example, the correct query would be 'WHERE t_id IS NULL'; the 'isnull'
lookup method in Django's database API implements this directly (and,
if you do 'isnull=False', implements the opposite, 'WHERE t_id IS NOT
NULL').

Documentation for it is here:
http://www.djangoproject.com/documentation/db_api/#isnull

-- 
"May the forces of evil become confused on the way to your house."
  -- George Carlin

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users
-~--~~~~--~~--~--~---



Re: Select all instances of a model that have a null ForeignKey value

2006-09-09 Thread cyberco

Thank you! That is a pretty obscure method.


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users
-~--~~~~--~~--~--~---



Re: Select all instances of a model that have a null ForeignKey value

2006-09-09 Thread John DeRosa

cyberco wrote:
> Given:
> 
> =Models==
> class T(models.Model):
> pass
> 
> class Y(models.Model):
> t = models.ForeignKey(T, blank=True, null=True)
> 
> 
> I want to select all instances of a Y that have a null value for t. I
> would say that it should be...
> 
> 
> Y.objects.filter(t_id=None)
> 
> 
> 
> ...but that returns all instances of T although some instances
> definitely have a value for t.

...filter(t__isnull=True)


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users
-~--~~~~--~~--~--~---



Select all instances of a model that have a null ForeignKey value

2006-09-09 Thread cyberco

Given:

=Models==
class T(models.Model):
pass

class Y(models.Model):
t = models.ForeignKey(T, blank=True, null=True)


I want to select all instances of a Y that have a null value for t. I
would say that it should be...


Y.objects.filter(t_id=None)



...but that returns all instances of T although some instances
definitely have a value for t.


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users
-~--~~~~--~~--~--~---