Opened 17 years ago

Closed 17 years ago

#4681 closed (wontfix)

filter on foreign keys with None doesn't return anything

Reported by: Patrick Lauber <patrick.lauber@…> Owned by: Adrian Holovaty
Component: Database layer (models, ORM) Version: dev
Severity: Keywords: filter foreignkey None
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

At the moment you have to do it like this:

class Category(models.Model):
    name = models.CharField(core=True, maxlength=64)
    parent = models.ForeignKey('self', blank=True, null=True, related_name='child')

def getNavigation(request,parent):
    if parent==None:
        return Category.objects.filter(parent__id__isnull=True).order_by('-priority')
    else:
        return Category.objects.filter(parent__id=parent).order_by('-priority')

I think for a newbie to Databases like me this not very intuitive. Wouldn't it be possible to make the isnull check automatically behind the scenes if the argument is None or an empty String?

Change History (1)

comment:1 by mir@…, 17 years ago

Component: Core frameworkDatabase wrapper
Resolution: wontfix
Status: newclosed

Hi Patrick, you're a bit late: We had this discussion already months ago. People who know SQL find it actually more intuitive as it is, and that's a pretty basic knowledge for writing database applications.

You can use the django developers mailing list in case you want to continue discussing this.

BTW, you probably meant {{filter(parent_id...)}} with only a single underscore ;-)

Note: See TracTickets for help on using tickets.
Back to Top