我不知道将数据添加到查询集会如此困难。就像,如果它不是直接来自数据库,那么它也可能不存在。即使我注释,新字段也是二等公民,并不总是可用。
为什么不序列化捕获我的注释字段?
模型
class Parc(models.Model):
# Regular Django fields corresponding to the attributes in the
# world borders shapefile.
prop_id = models.IntegerField(unique=True) # OBJECTID: Integer (10.0)
shp_id = models.IntegerField()
# GeoDjango-specific: a geometry field (MultiPolygonField)
mpoly = models.MultiPolygonField(srid=2277)
sale_price = models.DecimalField(max_digits=8, decimal_places=2, null=True)
floorplan_area = models.DecimalField(max_digits=8, decimal_places=2, null=True)
price_per_area = models.DecimalField(max_digits=8, decimal_places=2, null=True)
nbhd = models.CharField(max_length=200, null=True)
# Returns the string representation of the model.
def __str__(self): # __unicode__ on Python 2
return str(self.shp_id)
Run Code Online (Sandbox Code Playgroud)
询问:
parcels = Parc.objects\
.filter(prop_id__in=attrList)\
.order_by('prop_id') …Run Code Online (Sandbox Code Playgroud) 我正在尝试将 aqueryset作为JSON对象传递:
structure=Fund.objects.all().values('structure').annotate(total=Count('structure')).order_by('-total')
但是,querysets不是Json Serializable因此,我修改了我的代码:
from django.core import serializers
structure=serializers.serialize('json',Fund.objects.all().values('structure').annotate(total=Count('structure')).order_by('-total'))
Run Code Online (Sandbox Code Playgroud)
但我收到此错误:AttributeError: 'dict' object has no attribute '_meta'这是我的查询集:<QuerySet [{'total': 106, 'structure': 'Corp'}, {'total': 43, 'structure': 'Trust'}, {'total': 2, 'structure': 'OM'}, {'total': 0, 'structure': None}]>