Django Tables2显示不正确的真值

时间:2015-01-21 17:54:23

标签: django django-tables2

我对Django 1.7和Django-Tables2(0.15.0)有一个非常令人沮丧的问题。

我的模特:

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)  

来自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', )

来自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))

我遇到的问题是,无论搜索在表中返回1个结果还是12个,所有布尔值都显示为True(复选标记),无论它们在数据库中是什么。

我不知道它是否相关,但我的项目是以Django 1.6开始的,我在游戏中很晚才将其迁移到Django 1.7。除此之外,一切都很好。布尔值存储在我的数据库中,因为' t'或者' f'。有什么想法吗?

1 个答案:

答案 0 :(得分:0)

好的,我终于明白了。问题实际上是我的choices选项。这就是我将choices定义为:

YES_NO = (
    (True, 'Yes'),
    (False, 'No'),
)

我开始攻击django-tables2/columns/booleancolumn.py代码。当我将render函数设置为仅返回给定的值时,我意识到值为“是”或“否”。由于代码会使用bool()转换这些值,因此始终返回True

如果没有一些自定义,我认为Django-Tables2不能处理BooleanField上的“选择”。