(tables) Handling boolean fields with django-tables & mysql

304 views
Skip to first unread message

WaXGooN

unread,
Apr 29, 2009, 1:31:47 AM4/29/09
to Django Apps
MySQL handles Django BooleanFields as a tinyint field of either 0 or 1
(False / True, respectively). PostgresSQL on the other hand handles
Django BooleanFields as an actual boolean field.

This has the unintended side effect on django-tables where a boolean
field in MySQL will print out as 0 or 1, while in PostgresSQL, this
will print out as False/True. I know Django internally handles these
differences, and by the normal field accessing methods, it will be
consistent with printing out False / True.

What's the best way to work around this with django-tables?

AJ

unread,
May 3, 2009, 1:12:45 PM5/3/09
to Django Apps
Wouldn't you just use a filter? 0 and 1 still evaluate as False and
True. For display just use the yesno builtin filter
http://docs.djangoproject.com/en/dev/ref/templates/builtins/#yesno

Michael Elsdörfer

unread,
May 4, 2009, 4:36:02 PM5/4/09
to Django Apps
I haven't used Postgres yet, but this is strange, since django-tables
simple uses the Django ORM to access the fields. I.e. if
instance.field returns 0, then that's what django-tables should print
out.

Michael

Michael Elsdörfer

unread,
May 4, 2009, 4:36:45 PM5/4/09
to Django Apps
In fact, I just tried it now, using MySQL, I do get "1" and "0" for
boolean fields in a Django shell.

Michael

Mike RA9FTM

unread,
Jun 1, 2009, 12:10:11 AM6/1/09
to Django Apps
Hello. I have trouble with writing template with renaming True/False
to Yes/No only in boolean fields.

Here is sample from readme:

<table>
<tr>
{% for column in table.columns %}
<th><a href="?sort={{ column.name }}">{{ column }}</a></th>
{% endfor %}
</tr>
{% for row in table.rows %}
<tr>
{% for value in row %}
<td>{{ value }}</td>
{% endfor %}
</tr>
{% endfor %}
</table>


I can do {{ value|yesno }} but it is will be for all columns, but not
only for True/False.

Can anybody help me, please?

Michael Elsdörfer

unread,
Jun 1, 2009, 4:36:02 AM6/1/09
to Django Apps
I guess you have two options:

1) Output each field individually:

....
{% for row in table.rows %}
<tr>
<td>{{ row.foo }}</td>
<td>{{ row.bar }}</td>
<td>{{ row.boolvalue|yesno }}</td>
</tr>
{% endfor %}

2) Use a callable on the Python side:

def render(row):
return "Yes" if row['public_column_name'] else "No"
name_of_database_field = tables.Column(name="public_column_name",
data=render)

Mike RA9FTM

unread,
Jun 2, 2009, 10:35:29 AM6/2/09
to Django Apps
Hello.

Thank you very much
I am already created my own template filter MYyesno, but I think that
was not a good idea ))
Variant 2 is for me!

On 1 июн, 08:36, Michael Elsdörfer <elsdoer...@gmail.com> wrote:
Reply all
Reply to author
Forward
0 new messages