'How to save document with type "Reference" in Firestore using Python

How to save in firestore using python, a document with exactly type reference

I know in js, you can doing something like this:

let data = {
  name: 'productName',
  size: 'medium',
  userRef: db.doc('users/' + firebase.auth().currentUser.uid)
};
db.collection('products').add(data);

userRef being the reference.

what's in python the way to doing: db.doc('users/' + firebase.auth().currentUser.uid)



Solution 1:[1]

db.collection('users').document(firebase.auth().currentUser.uid)

I had the same question. in my case the follwoing case worked. Firebase recognize the Reference.

        batch = db.batch()
        for i in request.json['data']:
          i['ref'] = db.collection(u'component').document(i["supplier_item_number"])
          batch.set(stock_ref.document(), i)
        batch.commit()

Solution 2:[2]

Passing the document reference as the value in set() works. Example:

reference = db.collection('Company').document('Apple') 
db.collection('Product').document('iPhone').set({"company_id":reference},merge=True)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Marcel
Solution 2 Samrat Thapa