I am building a KivyMD
application and when the user enters the Admission No
, I need to write a program to bind the data in the relevant document to an MDLabel
. The MDLabel
should be in the following format.
Name : name
Admission No : 75
Phone No : 726464783
Database
.kv
MDFloatLayout:
pos_hint: {"center_x": .5, "center_y": .55}
TextInput:
hint_text: "Admission No"
id: ad_no
MDIconButton:
icon: 'magnify'
on_release:
app.search_st()
MDLabel:
text: " "
id : replace
.py
def search_st(self):
import firebase_admin
from firebase_admin import credentials
from firebase_admin import firestore
cred = credentials.Certificate("cred.json")
firebase_admin.initialize_app(cred)
self.db = firestore.client()
ad_no = self.root.get_screen('search_st').ids.ad_no.text
doc_ref = self.db.collection(u'Users').where(ad_no)
doc = doc_ref.get()
data = f'Document data: {doc.to_dict()}'
if doc.exists:
print(data)
self.root.get_screen('search_st').ids.replaced.text = data
else:
print(u'No such document!')
The program I have written is not working properly. There are so many issues.
According to the Firebase Documentation, you can retrieve data from this method.
doc_ref = db.collection(u'cities').document(u'SF')
doc = doc_ref.get()
if doc.exists:
print(f'Document data: {doc.to_dict()}')
else:
print(u'No such document!')
In your case you should give the ID
of the document you want to retrieve data.
Example :
002 - user 2
I suggest you to try this Code.
.kv
MDFloatLayout:
pos_hint: {"center_x": .5, "center_y": .55}
TextInput:
hint_text: "Name"
id: name
TextInput:
hint_text: "Admission No"
id: ad_no
MDIconButton:
icon: 'magnify'
on_release:
app.search_st()
MDLabel:
text: " "
id : name_replaced
MDLabel:
text: " "
id : ad_no_replaced
MDLabel:
text: " "
id : phone_replaced
.py
def search_st(self):
name = self.root.get_screen('search_st').ids.name.text
ad_no = self.root.get_screen('search_st').ids.ad_no.text
doc_name = ad_no + " - " + name
doc_ref = self.db.collection(u'Users').document(doc_name)
doc = doc_ref.get()
if doc.exists:
Name = str(doc.to_dict()['Name'])
Admission_No = str(doc.to_dict()['Admission_No'])
Phone_Number = str(doc.to_dict()['Phone_Number'])
self.root.get_screen('search_st').ids.name_replaced.text = Name
self.root.get_screen('search_st').ids.ad_no_replaced.text = Admission_Number
self.root.get_screen('search_st').ids.phone_replaced.text = Phone_Number
else:
print('No such document!')