I'm having a pretty frustrating problem with Django 1.7 and Django-Tables2 (0.15.0).
My model:
class Xuser(TimeStampedModel):
number = models.CharField(max_length=6, primary_key=True)
moniker = models.CharField(max_length=32)
first_name = models.CharField(max_length=32)
last_name = models.CharField(max_length=32)
full_name = models.CharField(max_length=128)
department = models.CharField(max_length=32)
citizenship = models.CharField(max_length=32)
location = models.CharField(max_length=32)
employee_type = models.CharField(max_length=32)
title = models.CharField(max_length=128, blank=True)
mail = models.EmailField(max_length=32)
open_account_authorized = models.BooleanField(choices=YES_NO, default=None)
open_account_enabled = models.BooleanField(choices=YES_NO, default=None)
root_authorized = models.BooleanField(choices=YES_NO, default=None)
From forms.py:
class AdminXuserTable(tables.Table):
number = tables.LinkColumn('accountadmindetail', args=[tables.A('number')])
class Meta:
model = Xuser
attrs = {"class": "adminsearchresults"}
fields = ('number', 'first_name', 'last_name', 'open_account_authorized', 'open_account_enabled',
'root_authorized', )
From views.py:
def accountadmin(request):
if request.method == "POST":
try:
searchtype = request.POST.get('search')
value = request.POST.get('value')
xusers = Xuser.objects.all()
if searchtype == 'number':
results = xusers.filter(number__startswith=value)
elif searchtype == 'group':
results = xusers.filter(department__contains=value)
elif searchtype == 'first':
results = xusers.filter(first_name__contains=value)
elif searchtype == 'last':
results = xusers.filter(last_name__contains=value)
elif searchtype == 'moniker':
results = xusers.filter(moniker__contains=value)
else:
messages.error(request, ERRMSG_NO_POST)
results = None
if results:
resultstable = AdminXuserTable(results)
else:
resultstable = None
except IndexError:
messages.error(request, ERRMSG_NO_POST)
return HttpResponseRedirect(reverse('error'))
else:
resultstable = None
return render_to_response('includes/accountadmin.html',
{"resultstable": resultstable, },
context_instance=RequestContext(request))
The problem that I am having is that whether the search returns 1 result or a dozen in the table, all of the Boolean values are shown as True (check mark), regardless of what they are in the database.
I don't know if it is relevant but my project began as a Django 1.6 and I migrated it over to Django 1.7 pretty late in the game. Everything is working fine except for this. Boolean values are stored in my database as 't' or 'f'. Any ideas?
Okay, i finally figured it out. The issue was actually with my choices
option. This is what i had my choices
defined as:
YES_NO = (
(True, 'Yes'),
(False, 'No'),
)
I started hacking through the django-tables2/columns/booleancolumn.py
code. When i set the render
function to just return the value given to it, i realized that the value was "Yes" or "No". Since the code converts these values with bool()
it was always returning True
.
Without some customization i don't think Django-Tables2 can handle "choices" on a BooleanField
.