Python datetime.date.today() not formatting inside sqllite3

Question:

In my database query which is executed with the sqlite3 module I insert a new row of data which includes a date field.

The problem is when getting todays date with datetime.date.today().strftime('%Y-%m-%d') which outputs '2023-02-06' (expected output), it changes inside the database to '2015'. Why does this happen?

This is a Django project so that is where i created the model for the database.

models.py

class User(models.Model):
    ...
    date_joined = models.DateField('%Y-%m-%d')
    ...

database.py

def add_user(self, email, password):
    date = datetime.date.today().strftime('%Y-%m-%d')
    self.cursor.execute(f"""
        INSERT INTO App_user ('username','email','password', 'email_preference', 'region', 'date_joined')
        VALUES ('{username}', '{email}', '{password}', 'All', 'None', {date})
    """)
    self.con.commit()
Asked By: logan_9997

||

Answers:

You should not have the date field on the client side when you create the user. This field in Django is automatically saved in the database by defining DateField and registering every user.
You can also use the following code snippet to record the user’s creation date as well as the date the user updated his profile.

created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
Answered By: Mohammad Nazari
Categories: questions Tags: , , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.