Reputation: 107
I am querying my database using the following command in python:
self.deleted_packages = model.Session.query(
model.Package).filter_by(state=model.State.DELETED)
And I am accessing the above value in pkg as show below:
for pkg in self.deleted_packages:
The output in pkg is :
<Package id=563e0da3-6916-4158-bc9f-f11bf92d3240
name=asdacaasas
title=asdacaasas
version= url= author= author_email= maintainer= maintainer_email= notes=
license_id=cc-by
type=dataset
owner_org=9b89900e-c4b8-413d-a8fe-edbe08641022
creator_user_id=30ad2d1c-f133-4458-9947-535e2718a898
metadata_created=2019-05-21 13:08:52.322015
metadata_modified=2019-05-21 13:08:59.946710
private=True
state=deleted
revision_id=5251e583-3a71-4189-8d4e-aaac8ac69927>
I just require the package id in a variable. And when I try to do the following: p1=[x[0] for x in pkg]
I get the following error:
p1=[x[0] for x in pkg]
TypeError: 'Package' object is not iterable
Does someone know how can i store the value of just id : 563e0da3-6916-4158-bc9f-f11bf92d3240
in a variable from the above query result.?
Upvotes: 1
Views: 13938
Reputation: 587
You should read the docs for SQLalchemy (I am getting this context from the comments).
There are a few examples examples that can highlight how you could get around your issue.
An example from the documentation linked in this answer (not relevant to your exact case!):
>>> for row in session.query(User, User.name).all():
... print(row.User, row.name)
With the output being:
<User(name='ed', fullname='Ed Jones', nickname='eddie')> ed
<User(name='wendy', fullname='Wendy Williams', nickname='windy')> wendy
<User(name='mary', fullname='Mary Contrary', nickname='mary')> mary
<User(name='fred', fullname='Fred Flintstone', nickname='freddy')> fred
So in your case the pkg
variable (an object of the Package
class) might be similar to the User
object in the example above and the name
contents is accessed by row.name
.
So your case might have something like pkg.id
Upvotes: 3