I’m pretty new to python so it’s prolly a stupid mistake i made.
I’m making an api which can be called using an external link. when calling the link my django server gives the following error: “‘RawQuerySet’ object has no attribute ‘key'”
Below is the code in which the error occures (definition of request, sql request and the model i use).
the error happens when i try to call planning.key. I believe django doesn’t know which type of object it is and doesn’t know how to retrieve data from it.
Hopefully somebody can see what i’m doing wrong.
code in which it’s going wrong
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
model
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()returnsRawQuerySetinstance, which is different from model instance.You need to iterate or slice on the
RawQuerySetto access actual model instance and its properties