I've a collection named XYZ in my firestore. And there are 500 documents with different fields in it.
I have to delete multiple documents using a where clause from the collection.
cred = credentials.Certificate('XXXX')
app = firebase_admin.initialize_app(cred)
db = firestore.Client()
batch = db.batch()
doc_ref = db.collection('collection_name').where(u'month', '==', 07).get()
for doc in doc_ref:
batch.delete(doc)
batch.commit()
I tried this but ending up with an error
AttributeError: AttributeError: 'DocumentSnapshot' object has no attribute '_document_path'
Looking for help!
You are passing a DocumentSnapshot object to batch.delete(), which is not allowed. You must pass a DocumentReference object instead, which can be found in a property of a DocumentSnapshot.
batch.delete(doc.reference)
Related
I need to update a bunch of documents in a Firestore database.
I'm successfully retrieving them by using a query, but now I should update the same field in each of them and I'm having troubles with it.
This is what I tried:
def main():
db_credentials = "some_local_file.json"
cred = credentials.Certificate(f"{db_credentials}")
firebase_admin.initialize_app(cred)
db = firestore.client()
user_id = "user_ID_000"
doc_ref_generator = db.collection(u'CollectionName').where(u'UID', u'==', user_id).where(u'Status', u'==', "Active").stream()
for doc_ref in doc_ref_generator:
doc_ref.update({u"Status": u"non_active"})
if __name__ == "__main__":
main()
and I'm getting the error:
File "/Users/user_name/projects/xxx/firestore_check.py", line 17, in main
doc_ref.update({u"Status": u"non_active"})
AttributeError: 'DocumentSnapshot' object has no attribute 'update'
You also have to specify the collection reference and get the document id when iterating the stream(). See sample code below:
# Collection Reference
col_ref = db.collection(u'CollectionName')
doc_ref_generator = col_ref.where(u'UID', u'==', user_id).where(u'Status', u'==', "Active").stream()
for doc_ref in doc_ref_generator:
# Document Reference
doc = col_ref.document(doc_ref.id)
doc.update({u"Status": u"non-active"})
or you could use the reference property to get the document reference from the DocumentSnapshot:
for doc_ref in doc_ref_generator:
# doc = col_ref.document(doc_ref.id)
doc_ref.reference.update({u"Status": u"non-active"})
You can checkout this documentations for more information.
Update a document
Reference
I am listening to changes on the database for collection_group
I cannot access ref (which has path) of a DocumentSnapshot I keep getting the error:
AttributeError: 'DocumentSnapshot' object has no attribute 'ref'
Here is my code:
doc_ref = firestore_db.collection_group(u'collection_name')
doc_ref.on_snapshot(self.__get_snapshot(args))
This is my __get_snapshot method:
def __get_snapshot(self, args):
def on_snapshot(doc_snapshot, changes, read_time):
for doc in doc_snapshot: #crashes
print(u'Received document snapshot: {}'.format(doc.ref))
for change in changes:
if(change.type.name == "MODIFIED"):
print(change.document.ref) #crashes
print(change.document.get("field"))#this works fine
return on_snapshot
The API documentation for DocumentSnapshot says that the reference of the document can be found in its reference property. So you will want to use this: doc.reference.
I'm working with pyoai library on python3.7 to harvest metadata using oai-pmh protocol but i'm getting troubles at the moment of read list of records
from oaipmh.client import Client
from oaipmh.metadata import MetadataRegistry, oai_dc_reader
URL = 'http://revista-iberoamericana.pitt.edu/ojs/index.php/Iberoamericana/oai'
registry = MetadataRegistry()
registry.registerReader('oai_dc', oai_dc_reader)
client = Client(URL, registry)
for record in client.listRecords(metadataPrefix='oai_dc'):
print(record)
i was especting a kind of xml file on tuples, but the results are like this:
(<oaipmh.common.Header object at 0x00000251FAA16A20>, <oaipmh.common.Metadata object at 0x00000251FAA160B8>, None)
(<oaipmh.common.Header object at 0x00000251FA9DB5C0>, <oaipmh.common.Metadata object at 0x00000251FA9C6518>, None)
(<oaipmh.common.Header object at 0x00000251FA9DB0F0>, <oaipmh.common.Metadata object at 0x00000251FA9DB208>, None)
could you tellme if i'm forgetting something
You can use record[1].getMap()
https://tinker.edu.au/resources/recipes/api-via-oai-pmh/
I'm trying to write a program that gets a json object from an api call and then loads that into a database. sqlalchemy keeps throwing the error AttributeError: 'TextClause' object has no attribute 'implicit_returning' when I compile stmt or try to just execute it on its own.
def load_results(self):
connect = self.engine.connect()
for ru in self.json_ru:
counties = ru['counties']
for county in counties:
transaction = connect.begin()
params = {"reportingunitName": ru['reportingunitName'],
"geoCode": ru['geocode'],
"townsTotal": ru['towns'],
"townsReporting": ru['townsReporting'],
"cableName": county['cableName'],
"unitCount": county['unitCount']
}
stmt = expression.insert('{}.raw_feed'.format(self.state)).values(params)
compiled_stmt = stmt.compile(self.engine)
connect.execute(compiled_stmt)
transaction.commit()
Before I had a query I was just feeding into connect.execute, but the strings that were coming in as values were being read as column names, so I tried the above instead. Also using Postgres flavor.
I am using web controller in odoo 8 to make a REST API that will get some data and return values from the database. The problem is that I am not able to get the database from the builtin ORM.
I tried to call osv.pool.get() but gave me the error:
AttributeError: type object 'Model' has no attribute 'pool'
Odoo 8 apparently uses recordsets, but I can't use it too, and couldn't find anything usefull on docs.
How can I browse database data on web controller?
My code:
class TestWebService(http.Controller):
#http.route('/test', type='http', auth='none')
def test(self):
objects = osv.osv.pool.get("some_table")
# I need to get the objects from some_table and search them
return "Hello World"
Try Following
myobj = request.env['some.table']