Django values_list与选择字段 [英] Django values_list with choices field

查看:478
本文介绍了Django values_list与选择字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

过滤对象中的 values_list 确实对我在django视图内提供解决方案很有帮助。

The values_list in filtering object, really helps me a lot in providing solution within django view.

我的代码如下所示,并且该代码有效:

My code is like the following and this one works:

values_list_ac = realdata.objects.filter(product = '1').values_list('company', 'brand', 'created_by__username')

而用户名字段存在于不同

while username is the field exists in different model outside the current realdata model.

但是下面的代码不起作用,因为我想显示 ac_type ,它基于同一 realdata 模型中的选择字段。 (我尝试使用与模板相同的解决方案来解决该问题):

But the following code doesn't work, for I want to show the value of ac_type, which based on choices field within the same realdata model. (I try to solve it by using the same solution which work in template):

values_list_ac = realdata.objects.filter(product = '1').values_list('company', 'brand', 'created_by__username', 'get_ac_type_display')

除了 get_ac_type_display 以外,还有其他解决方案来显示字段值吗?

Is there a solution other than get_ac_type_display to show the field value?

我真的很感谢棚子

编辑:
这是我的模型:

This my model:

    class realdata(models.Model):
        company = models.CharField(max_length=60, verbose_name="Company")
        brand = models.CharField(_('brand'), max_length=60) 
        model = models.CharField(max_length=60)
        type_choices = (
            (u'1', u'Inverter'),
            (u'2', u'Non-Inverter'),
        )
        ac_type = models.CharField(max_length=60, verbose_name="Type", choices=type_choices)
        created_by = models.ForeignKey(User)

非常感谢!

推荐答案

values_list 函数将只获取存储在数据库中的值。在模型的字段上定义选择时,它将存储元组的第一个值,因此这将是您要检索的内容。

The values_list function will just get the values stored in the database. When defining choices on your model's field, it will store the first value of the tuple, hence this will be what you'll retrieve.

这意味着您必须查看在选项元组中确定项目的显示值。 <$ c $的目的c> get_foo_display 就是为了给您这种人类可读的值,但是它需要一个模型实例来进行处理。

This means that you have to look at the choices tuple to determine the display value for the item. The purpose of the get_foo_display is to give you this human-readable value, but it needs a model instance to work on.

所以,自己解决此问题的一种方法是检查选择,并相应地转换数据。以下应该能够做到这一点:

So, a solution to resolving this yourself would be to inspect the choices, and convert the data accordingly. The following should be able to do this:

result = []
for p in realdata.objects.filter(product='1').values_list(
                          'company', 'brand', 'created_by__username', 'ac_type'):
    choice = {k: v for k, v in realdata.type_choices}[p[-1]]
    result.append(list(p[:-1]) + [choice])

结果变量将包含转换后的列表。需要新的变量,因为 values_list 函数将返回一个元组列表。后者是不变的。另外,请注意将要解析的值作为 values_list 调用中的最后一项,或将上面的值进行调整以匹配。

The result variable will contain the converted list. The new variable is needed because the values_list function will return a list of tuples; the latter being unmutable. Also, take care to have the value you'll want to resolve as the last item in your values_list call, or adapt the above to match.

这篇关于Django values_list与选择字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆