'storing data in mongodb - want to store as string and not a date
I am using the following code to store information in a document in mongodb:
# Opening JSON file
f = open('quiz.json')
# returns JSON object as
# a dictionary
data = json.load(f)
# Closing file
f.close()
username = data["username"]
first_name = data["first_name"]
last_name = data["last_name"]
password = data["password"]
email = data["spare_1"]
role = "student"
expires = "31-12-2022"
user = User(spare_1=email, username=username, first_name=first_name,
last_name=last_name, password=password, role=role, expires=expires)
user.save()
The problem I have is that the 'expires' field is uploading to mongodb in the format '2022-12-31T00:00:00.000+00:00' which causes me problems later as some dates uploaded form a csv file are uploading as only "31-12-2022". As it then is storing them differently, later when I go try to:
datet = session['expires']
datet = str(datet)
account_expires = datetime.strptime(datet,"%d-%m-%Y")
I get the error:
ValueError: time data '2022-12-31 00:00:00+00:00' does not match format '%d-%m-%Y'
I don't have enough knowledge to format these dates, so it would be easiest I get the expiry date added as the string "31-12-2002" only? Any help would be appreciated.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
