1

i have a very simple IntegerField in one of my models.

models.py

## Imports

from django.db import models

class UserModel(models.Model)
    name = models.CharField(max_length=50)


class NumberModel(models.Model)
    user = models.ForeignKey(UserModel)
    number = models.IntegerField(blank=True, null=True) # This is the IntegerField

in the views i am passing the number model itself:

views.py

def change_user_number(request):
    number = get_object_or_404(models.NumberModel, user=request.user)
    return render_to_response('change_number.html', {'number' : number }, context_instance=RequestContext(request))

Now when i use the value of the the above mentioned Integer Field in one of my custom forms, it shows up as follows:

Text Field with blank IntegerField's Value

In the template i have something like this:

template

<form id="" action="" method="POST">
    <p><label for="user_number"></label></p>
    <p><input type="text" name="user_number" id="user_number" value="{{ number.number }}" />
    <input type="submit" value="submit" />
</form>

I would want to show the value only if the value exists. I am quite sure that the value is saved as null in the database but why does it return "None".

I have also tried the following with no success.

<input type="text" name="user_number" id="user_number" {% if number.number == "None" %} value="" {% else %} value="{{ number.number }}" {% endif %} />
3
  • Does it read Null on the field in the database? Commented Dec 20, 2012 at 10:35
  • yes @RickardZachrisson the value shows as null in the db Commented Dec 20, 2012 at 10:37
  • though if i do type(number) from the shell it returns NoneType Commented Dec 20, 2012 at 10:42

2 Answers 2

3

When number.number is None, it will be stored as null in the database. As Daniel says in the comments, None is not the same as the string "None", which is why your comparison does not work.

I recommend you use the default_if_none template filter.

{{ number.number|default_if_none:"" }}

This distinguishes between when the value is 0 and None.

Sign up to request clarification or add additional context in comments.

1 Comment

thanks @Alasdair i did not know about this template tag. This helps !
1

Check if value exist:

{% if number.number %}

4 Comments

do you mean {% if number.number %} ? as in the case above number is the NumberModel instance which ofcourse exists.
yeah i have already checked it as metioned in the question above. but the "None" still shows up inside the input field.
@Amyth but you checked if it was equal to the string "None". It isn't, it's equal to None. You should check as Like it recommends here.
@Likeit my bad i checked for "None", {% if number.number %} works. Much appreciated.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.