On Sat, 2006-05-20 at 04:15 -0700, PythonistL wrote:
> Question 1
> I noticed in MySQL  that if a  table field  has NULL value, then I can
> test in Python with  that filed with None.
> In otherwords NULL in MySQL is the same as None. in Python
> Is it so or am I wrong?

That is the way Django does the mapping, yes. If the database field is
NULL, the attribute in the object will be set to None.

> Question 2.
> I also would like to test NULL values in my templates. So I would like
> to use something like this:
>  {% ifequal  Price NULL %}
> .....
> ....
> {%else%}
> .....
> {% endifequal %}
> 
> Is that possible? NULL doesn't work there neither None.What shall I use
> in the template  to test NULL values in MySQL?

It is probably making life a little harder than necessary to think of
this as testing against the database. You are testing against an
attribute in the model, really. So think in Python terms, not SQL inside
templates.

The simplest way to test this is just to test if it's True or False, so
"{% if Price %}" should work. The only way something will not pass this
if-test is if it is non-existant, None, and empty string or a boolean
field with a value of False. So you {% else %} block will only be
executed if Price was NULL in the database.

Best wishes,
Malcolm


X-Google-Language: ENGLISH,ASCII-7-bit
Received: by 10.54.73.16 with SMTP id v16mr144293wra;
        Sat, 20 May 2006 05:00:08 -0700 (PDT)
Return-Path: <[EMAIL PROTECTED]>
Received: from bigben2.bytemark.co.uk (bigben2.bytemark.co.uk [80.68.81.132])
        by mx.googlegroups.com with ESMTP id v23si361820cwb.2006.05.20.05.00.06;
        Sat, 20 May 2006 05:00:08 -0700 (PDT)
Received-SPF: neutral (googlegroups.com: 80.68.81.132 is neither permitted nor 
denied by best guess record for domain of [EMAIL PROTECTED])
Received: from sharp.pointy-stick.com ([80.68.90.23])
        by bigben2.bytemark.co.uk with esmtp (Exim 4.52)
        id 1FhQ7q-0001dG-Od
        for django-users@googlegroups.com; Sat, 20 May 2006 12:00:06 +0000
Received: from counterweight.tredinnick.org (cust7071.nsw01.dataco.com.au 
[203.171.88.159])
        by sharp.pointy-stick.com (Postfix) with ESMTP id 2A2CDC38C
        for <django-users@googlegroups.com>; Sat, 20 May 2006 22:00:02 +1000 
(EST)
Subject: Re: NULL  vs.None
From: Malcolm Tredinnick <[EMAIL PROTECTED]>
To: django-users@googlegroups.com
In-Reply-To: <[EMAIL PROTECTED]>
References: <[EMAIL PROTECTED]>
Mime-Version: 1.0
Content-Type: text/plain
Date: Sat, 20 May 2006 21:59:53 +1000
Message-Id: <[EMAIL PROTECTED]>
X-Mailer: Evolution 2.6.1 (2.6.1-1.fc5.2) 

On Sat, 2006-05-20 at 04:15 -0700, PythonistL wrote:
> Question 1
> I noticed in MySQL  that if a  table field  has NULL value, then I can
> test in Python with  that filed with None.
> In otherwords NULL in MySQL is the same as None. in Python
> Is it so or am I wrong?

That is the way Django does the mapping, yes. If the database field is
NULL, the attribute in the object will be set to None.

> Question 2.
> I also would like to test NULL values in my templates. So I would like
> to use something like this:
>  {% ifequal  Price NULL %}
> .....
> ....
> {%else%}
> .....
> {% endifequal %}
> 
> Is that possible? NULL doesn't work there neither None.What shall I use
> in the template  to test NULL values in MySQL?

It is probably making life a little harder than necessary to think of
this as testing against the database. You are testing against an
attribute in the model, really. So think in Python terms, not SQL inside
templates.

The simplest way to test this is just to test if it's True or False, so
"{% if Price %}" should work. The only way something will not pass this
if-test is if it is non-existant, None, and empty string or a boolean
field with a value of False. So you {% else %} block will only be
executed if Price was NULL in the database.

Best wishes,
Malcolm



--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to