gpt4 book ai didi

python - 'RawQuerySet' 对象没有属性 'key'

转载 作者:行者123 更新时间:2023-11-28 23:01:40 26 4
gpt4 key购买 nike

我是 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请求

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'

最佳答案

Planning.objects.raw() 返回 RawQuerySet instance ,这与模型实例不同。

您需要对 RawQuerySet 进行迭代或切片以访问实际模型实例及其属性

qs = planning.objects.raw()
qs[0].key
for x in qs:
x.key

关于python - 'RawQuerySet' 对象没有属性 'key',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10817728/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com