Nehax
Nehax

Reputation: 49

Add a map to a Firestore doc using Python

I'm trying to add a map into an existing document in Firestore that already had data written into. I found out how to add data without overwriting existing data using merge, however, my data is registered as a string.

Here is the piece of code that is supposed to push that data:

def sendCustomer(self):
    db.collection("parameters").document("new_mlt_ben").set(self.__dict__, merge=True)

It is used on a custom object Customers that have only one parameter : name

class Customers(object):
    def __init__(self):
        self.name = [] #When I push the obj in the doc, name contain only one name,
                       #it is set as a list because I use it as a list on another function that get all
                       #map from the "new_mlt_ben" which are customers name on that context

Here is the piece of code where I actually use the function:

@app.route("/customer", methods=['GET', 'POST'])
def addCustomer():
    form = AjouterCustomer() #WTForm generated form
    if form.validate_on_submit():
        customer = Customers() #I create an empty customer obj
        customer.setName(form.name.data) #I set the name with the user input
        try:
            customer.sendCustomer() #I push the obj
            flash("Add with success", 'success') #Woohoo it worked
        except ZeroDivisionError:
            flash("Bip bap boop it's broken", 'danger') #Woopsi
        return redirect(url_for('ajouter'))
    return render_template('ajouter.html', title='Ajouter customer', form=form)

TL;DR

I want to add customer.name inside the doc new_mlt_ben as a map, but it is registered as a string.

Upvotes: 1

Views: 1117

Answers (1)

Nehax
Nehax

Reputation: 49

So I found out how to do, here is the solution for thoose who would like to know :

def sendCustomer(self):
    data = {
        self.name: {}
    }
    db.collection("parameters").document("new_mlt_ben").set(data, merge=True)

Upvotes: 3

Related Questions