我对python很陌生,所以这是我犯的一个愚蠢的错误。
我正在制作一个可以使用外部链接调用的api。调用链接时,我的django服务器出现以下错误:“'RawQuerySet'对象没有属性'key'”
下面是发生错误的代码(请求的定义,sql请求和我使用的模型)。 当我尝试调用planning.key时发生错误。我相信django不知道它是哪种类型的对象,也不知道如何从中检索数据。
希望有人能看出我做错了什么。
出错的代码
def get(request):
if not len(request.GET):
raise WeegNetError(101)
data = request.GET.copy()
usr = user.checkUser(data)
planning = libs.getPlanning(data['planningkey'])
if not planning:
raise WeegNetError(310)
fields = {
'key': planning.key,
'reference': planning.reference,
'startdate': planning.plan_start.strftime("%Y-%m-%d %H:%M:%S"),
'enddate': None if not p.plan_end else planning.plan_end.strftime("%Y-%m-%d %H:%M:%S"),
'product': planning.product,
'address_from': {
'relation': planning.rel_from,
'street': planning.street_from,
'housenr': planning.housenr_from,
'zipcode': planning.zipcode_from,
'city': planning.city_from
},
'address_to': {
'relation': planning.rel_to,
'street': planning.street_from,
'housenr': planning.housenr_to,
'zipcode': planning.zipcode_to,
'city': planning.city_to
}}
return HttpResponse(simplejson.dumps(fields))
sql request
def getPlanning(planningkey):
sqlQuery = """SELECT k.key,
pl.id,
pl.reference As ref,
pl.plan_start ,
pl.plan_end,
pr.title as product,
r1.name AS rel_from,
loc1.address AS street_from,
loc1.housenr AS housenrfrom,
loc1.zipcode AS zipcode_from,
loc1.city AS city_from,
r2.name as rel_to,
loc2.address AS street_to,
loc2.housenr AS housenr_to,
loc2.zipcode AS zipcode_to,
loc2.city AS city_to
FROM `planning` pl
LEFT JOIN `keys` k
ON pl.id = k.planning_id
LEFT JOIN `product` pr
ON pl.product_id = pr.id
LEFT JOIN `relations` r1
ON pl.from_owner = r1.id
LEFT JOIN `relations` r2
ON pl.to_owner = r2.id
LEFT JOIN `depot` dep1
ON pl.to_depot = dep1.id
LEFT JOIN `locatie` loc1
ON dep1.locatie_id = loc1.id
LEFT JOIN `depot` dep2
ON pl.from_depot = dep2.id
LEFT JOIN `locatie` loc2
ON dep2.locatie_id = loc2.id
WHERE pl.id = (SELECT planning_id
FROM `keys`
WHERE `key` = %(planningkey)s)
AND pl.deleted = 0"""
from models import Planning
try:
return Planning.objects.raw(sqlQuery, {'planningkey': planningkey})
except:
return None
模型
class Planning(models.Model):
id = models.AutoField(primary_key=True)
key = models.CharField(max_length=8, blank=True, null=True)
reference = models.CharField(max_length=255, blank=True, null=True)
plan_start = models.DateField(null=False)
plan_end = models.DateTimeField(null=True, default=None)
product = models.CharField(max_length=255, blank=True, null=False)
rel_from = models.CharField(max_length=255, blank=True, null=True)
street_from = models.CharField(max_length=255, blank=True, null=True)
housenr_from = models.CharField(max_length=10, blank=True, null=True)
zipcode_fom = models.CharField(max_length=10, blank=True, null=True)
city_from = models.CharField(max_length=255, blank=True, null=True)
rel_to = models.CharField(max_length=255, blank=True, null=True)
street_to = models.CharField(max_length=255, blank=True, null=True)
housenr_to = models.CharField(max_length=10, blank=True, null=True)
zipcode_to = models.CharField(max_length=10, blank=True, null=True)
city_to = models.CharField(max_length=255, blank=True, null=True)
class Meta:
db_table = u'planning'
答案 0 :(得分:4)
Planning.objects.raw()
返回RawQuerySet
instance,这与模型实例不同。
您需要对RawQuerySet
进行迭代或切片以访问实际模型实例及其属性
qs = planning.objects.raw()
qs[0].key
for x in qs:
x.key