简体   繁体   English

如何从 Django 中的查询集中检索值?

[英]How to retrieve value from queryset in django?

I am trying to retrieve different .values() from query sets but am having an issue with it returning the proper values.我正在尝试从查询集中检索不同的.values()但在返回正确值时遇到问题。 How to write my model so that I can retrieve attributes using the .values() method?如何编写我的model以便我可以使用.values()方法检索属性?

I have tried to change the model's __str__ method to return a dictionary but that does not work or I am doing it wrong.我试图更改模型的__str__方法以返回字典,但这不起作用或者我做错了。

class Settings(models.Model):
    bb_bonus_qualify = models.CharField(max_length=16, default=38.00)
    service_breakpoint = models.CharField(max_length=16, default=1700.00)

    def __str__(self):
        return '%s: %s, %s: %s' % (
            'bb_bonus', self.bb_bonus_qualify, 'service_breakpoint', self.service_breakpoint)

I would like to say Settings.objects.last().values('bb_bonus') and be returned the value which is self.bb_bonus_qualify .我想说Settings.objects.last().values('bb_bonus')并返回值为self.bb_bonus_qualify The common error I seem to get is: AttributeError: 'Settings' object has no attribute 'values'我似乎得到的常见错误是: AttributeError: 'Settings' object has no attribute 'values'

The problem here is that your .last() will retrieve the last Settings object.这里的问题是您的.last()将检索最后一个Settings对象。 You thus will call .values('bb_bonus') on the Settings object.因此,您将在Settings对象上调用.values('bb_bonus') Since a model has no .values(..) method, it will thus not return anything.由于模型没有.values(..)方法,因此它不会返回任何内容。

You can however retrieve the value of a certain column from a queryset, with:但是,您可以从查询集中检索某个列的值,使用:

Settings.objects.values_list('bb_bonus_qualify', flat=True).last()

We here thus use .values_list(..) [Django-doc] , this accepts the names of the columns as parameters.因此,我们在这里使用.values_list(..) [Django-doc] ,它接受列的名称作为参数。 It will then usually return a QuerySet of lists with these values.然后它通常会返回一个包含这些值的列表的QuerySet But if you specify one column;但是如果你指定列; then, as the documentation says:然后,正如文档所说:

If you only pass in a single field, you can also pass in the flat parameter.如果只传入单个字段,也可以传入flat参数。 If True , this will mean the returned results are single values , rather than one-tuples.如果为True ,这将意味着返回的结果是单个值,而不是一元组。

So that means we create a QuerySet of singular values, and we then will retrieve the last entry of that queryset.所以这意味着我们创建了一个奇异值的QuerySet ,然后我们将检索该查询集的最后一个条目。 Note that we do not fetch all the elements from the queryset, the .last() is "injected" in the query we perform on the database, so the result is the scalar value of that column for the last record.请注意,我们没有从查询.last()获取所有元素, .last()被“注入”到我们对数据库执行的查询中,因此结果是最后一条记录的该列的标量值。

The .values_list(..) thus needs to be performed before the .last() , since otherwise you are talking to a Settings object, not to a QuerySet .因此.values_list(..)需要在.last()之前执行,否则您将与Settings对象对话,而不是与QuerySet对话。

AFAIK __str__.values()无关 - 这里的问题是您需要获取特定项目之前指定值,而不是相反:

Settings.objects.values('bb_bonus').last()

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

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