Opened 16 years ago

Closed 16 years ago

#6696 closed (invalid)

saving boolean field

Reported by: imgrey Owned by: nobody
Component: Core (Other) Version: dev
Severity: Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Seems django behaves strange with boolean fields. Following is my observations.

code:

"""
models.py:
class Thread(models.Model):
    closed = models.BooleanField(default=False)
    def save(self):
	#...
	super(Thread, self).save()
"""

import sys
sys.path.append('/home/grey/src/df')
from django.db import *
from board.models import Thread
thread = Thread.objects.all()[0]
print "IN:", thread.closed
thread.closed = not thread.closed
thread.save()
thread = Thread.objects.all()[0]
print "OUT:", thread.closed

outputs:

IN: True
OUT: True

IN: True
OUT: False

IN: False
OUT: False

detailed:

IN: True
[{'time': '0.002', 'sql': 'SELECT "board_thread"."id","board_thread"."subject","board_thread"."forum_id","board_thread"."closed","board_thread"."csticky","board_thread"."gsticky","board_thread"."slug" FROM "board_thread" LIMIT 1 '}, {'time': '0.026', 'sql': 'SELECT 1 FROM "board_thread" WHERE "id"=2'}, {'time': '0.011', 'sql': 'UPDATE "board_thread" SET "subject"=\'mmnbmnbmnbmnbmnb\',"forum_id"=1,"closed"=false,"csticky"=false,"gsticky"=false,"slug"=\'mmnbmnbmnbmnbmnb\' WHERE "id"=2'}]
OUT: True
[{'time': '0.002', 'sql': 'SELECT "board_thread"."id","board_thread"."subject","board_thread"."forum_id","board_thread"."closed","board_thread"."csticky","board_thread"."gsticky","board_thread"."slug" FROM "board_thread" LIMIT 1 '}, {'time': '0.026', 'sql': 'SELECT 1 FROM "board_thread" WHERE "id"=2'}, {'time': '0.011', 'sql': 'UPDATE "board_thread" SET "subject"=\'mmnbmnbmnbmnbmnb\',"forum_id"=1,"closed"=false,"csticky"=false,"gsticky"=false,"slug"=\'mmnbmnbmnbmnbmnb\' WHERE "id"=2'}, {'time': '0.001', 'sql': 'SELECT "board_thread"."id","board_thread"."subject","board_thread"."forum_id","board_thread"."closed","board_thread"."csticky","board_thread"."gsticky","board_thread"."slug" FROM "board_thread" LIMIT 1 '}]

Change History (1)

comment:1 by Philippe Raoult, 16 years ago

Resolution: invalid
Status: newclosed

I assume you ran the code snippet 3 times in a row to get the output produced ? You should know that your code depends on the order of the thread in Thread.objects.all() which might not be defined. Try running the same code with thread = Thread.objects.get(id=2) and it should work.

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