I have something like this:

    class Node(Model):
        widgets = ForeignKey(Widget)

And I want to retrieve all nodes that have >0 widgets.  I have
an .extra() query like this:

    nodes = Node.objects.all().extra(
        select={
            'num_widgets': 'SELECT COUNT(*) FROM appname_node_widgets
WHERE appname_node_widgets.node_id = appname_node.id',
        }
    )

This works fine, since I can now do this:

    print nodes[0].num_widgets

But I can't filter or exclude on this field.  I try this:

    nodes = nodes.exclude(num_widgets=0)

Gives a "FieldError: Cannot resolve keyword 'num_widgets' into
field."  I understand that the fields created by the .extra() call are
not "real" fields attached to the model, but they are fields attached
to the QuerySet.  Is there a way to use fields created with .extra()
in subsequent filter/exclude calls?

My next guess is to drop down to custom SQL and write the whole query
out by hand, but I thought I'd check to see if I'm doing something
wrong here.  I thought I remembered being able to use .extra() fields
in later .filter() statements, but I might be imagining that.

Any thoughts?  Thanks!

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to