简体   繁体   English

django:带有查询集值的选择显示

[英]django: choice display with queryset values

I want to use get_status_display on template, from a queryset with just some fields from model.我想在模板上使用 get_status_display,来自一个只有模型中一些字段的查询集。

I have a model with a choices field (status).我有一个带有选择字段(状态)的模型。

class Operation(models.Model, ModelMixin):
    operation_area = models.ForeignKey('operations.OperationArea', null=True)

    created = models.DateTimeField(auto_now=True)
    indication = models.NullBooleanField()
    observation = models.TextField(blank=True, null=True)
    rate = models.IntegerField(blank=True, null=True)
    scheduling = models.DateTimeField(blank=True, null=True)
    status = models.IntegerField(choices=OPERATION_TYPE)

class OperationArea(models.Model, ModelMixin):
    campaign = models.ForeignKey('campaigns.Campaign')
    person = models.ForeignKey('registrations.Person')
    user = models.ForeignKey('authentication.User')

    area = models.CharField(max_length=1, choices=OPERATION_AREA)

My query is:我的查询是:

Operation.objects.filter(operation_area__user=user).values('id', 'created', 'operation_area__person__name', 'status')

From this a get a dict, but get_FOO_dislpay is a instance method and don't works on a dict.由此得到一个 dict,但 get_FOO_dislpay 是一个实例方法,不适用于 dict。 I don't want to use .only() because he brings operation_area object and to get the person.name he makes another query, like this operation.operation_area.person.name .我不想使用 .only() 因为他带来了operation_area对象并获取person.name他进行了另一个查询,就像这个operation.operation_area.person.name Any thoughts how to work with one query and get_FOO_display?任何想法如何处理一个查询和 get_FOO_display?

I think annotate() is what you looking for.我认为annotate()就是你要找的。

Annotates each object in the QuerySet with the provided list of query expressions.使用提供的查询表达式列表注释 QuerySet 中的每个对象。 An expression may be a simple value, a reference to a field on the model (or any related models), or an aggregate expression (averages, sums, etc.) that has been computed over the objects that are related to the objects in the QuerySet.表达式可以是一个简单的值、对模型(或任何相关模型)上的字段的引用,或者是在与模型中的对象相关的对象上计算出的聚合表达式(平均值、总和等)。查询集。

Each argument to annotate() is an annotation that will be added to each object in the QuerySet that is returned. annotate() 的每个参数都是一个注释,将添加到返回的 QuerySet 中的每个对象。

The aggregation functions that are provided by Django are described in Aggregation Functions below. Django 提供的聚合函数在下面的聚合函数中描述。

Annotations specified using keyword arguments will use the keyword as the alias for the annotation.使用关键字参数指定的注释将使用关键字作为注释的别名。 Anonymous arguments will have an alias generated for them based upon the name of the aggregate function and the model field that is being aggregated.匿名参数将根据聚合函数的名称和正在聚合的模型字段为它们生成一个别名。 Only aggregate expressions that reference a single field can be anonymous arguments.只有引用单个字段的聚合表达式才能是匿名参数。 Everything else must be a keyword argument.其他所有内容都必须是关键字参数。

You should use:你应该使用:

operations.annotate(person_name=F('operation_area__person__name')) 

Take a look at https://docs.djangoproject.com/en/2.0/ref/models/querysets/#annotate .看看https://docs.djangoproject.com/en/2.0/ref/models/querysets/#annotate

I know it's quite late to reply.我知道现在回复已经很晚了。 Nevertheless, I think my solution is worth sharing:尽管如此,我认为我的解决方案值得分享:

def display_value(choices,field):
    options = [
         When(**{field: k, 'then': Value(v) })
          for k,v in choices
    ]
    return Case(
        *options,output_field=CharField()
    )

and you can use:你可以使用:

my_field = {'field_value': display_value(CHOICES, 'field'),}
YourModel.objects.annotate(**my_field)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM