是否可以使用Django的QuerySet.values_list引用属性? [英] Is it possible to reference a property using Django's QuerySet.values_list?
问题描述
class Person(models。模型):
first_name = models.CharField(max_length = 30)
last_name = models.CharField(max_length = 30)
def _get_full_name(self):
return %s%s%(self.first_name,self.last_name)
full_name = property(_get_full_name)
当我创建查询时,我想引用该属性。例如:
people = Person.objects.all()。values_list('full_name')
不幸的是,Django产生以下FieldError:
FieldError:无法将关键字'full_name'解析为字段
长篇小说,是否可以通过values_list访问自定义属性() 方法?如果没有,有没有人有任何建议如何最好地补救这个问题?
全名不是django中的一个字段模型,这是不可能的。
您可以使用列表推导
[person.objects.all()中person的人名]
I have a custom property on my Django model that returns the full name of a Person:
class Person(models.Model):
first_name = models.CharField(max_length=30)
last_name = models.CharField(max_length=30)
def _get_full_name(self):
return "%s %s" % (self.first_name, self.last_name)
full_name = property(_get_full_name)
When I create a query, I'd like to reference that property. For example:
people = Person.objects.all().values_list('full_name')
Unfortunately, Django yields the following FieldError:
FieldError: Cannot resolve keyword 'full_name' into field
Long story short, is it possible to access a custom property via the values_list() method? If not, does anyone have any suggestions on how to best remedy this problem?
full name is not a field in the django model, it is not possible. you can use list comprehensions
[person.fullname for person in Person.objects.all() ]
这篇关于是否可以使用Django的QuerySet.values_list引用属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!