Field exists but django tells me "is not defined"

41 views
Skip to first unread message

gregory semah

unread,
Oct 6, 2011, 7:54:26 AM10/6/11
to Django users
Hi All,

I'm dealing with really weird problem

My model
class Projet(models.Model):
...
fact = models.PositiveIntegerField('Numéro de
Facture',null=True,blank=True)
...
...

A simple view from this model is
def facture(request, id=None):
projet = Projet.objects.all()

So as far as I am, everything worked as expected
Now I wanted to change
projet = Projet.objects.all() to projet =
Projet.objects.filter(fact=None)
it will send projet filtered as expected (all projects with no number
assigned to the fact field) to template
So the template display this as expected...

Now, when I want to filter only projets with a assigned number to the
fact field its return

NameError at /facture/
global name 'fact' is not defined

Have i missed something?
I'm used with this kind of operation with django , but right now I'm a
bit lost...


Thanks in advance

Gregory Sémah

gregory semah

unread,
Oct 6, 2011, 7:56:45 AM10/6/11
to Django users
I mean when i write "Projet.objects.filter(fact!=None)" instead of
"Projet.objects.filter(fact!=None)"

The NameError appears

Thanks

gregory semah

unread,
Oct 6, 2011, 8:00:11 AM10/6/11
to Django users
Sorry my mistake again...

I mean when i write "Projet.objects.filter(fact!=None)" instead of
"Projet.objects.filter(fact=None)"

Thanks

gregory semah

unread,
Oct 6, 2011, 8:12:15 AM10/6/11
to Django users
Ok isolved with
projet = Projet.objects.filter(fact__isnull=False)

Everything 's back to normal :)

Daniel Roseman

unread,
Oct 6, 2011, 8:15:13 AM10/6/11
to django...@googlegroups.com
On Thursday, 6 October 2011 13:00:11 UTC+1, gregory semah wrote:
Sorry my mistake again...

I mean when i write "Projet.objects.filter(fact!=None)" instead of
"Projet.objects.filter(fact=None)"

Thanks


You can't use != in filter, as it's a function call. You need to do `filter(fact__isnull=False)` or `exclude(fact=None)`.
--
DR. 
Reply all
Reply to author
Forward
0 new messages