0

My database is MySQL. I am trying to set default value to current date time to created_at field below in the migration code. Due to some reasons, the default value is showing nothing in the database side, likewise, is_Active is having same issue. Am I missing anything?

I meant, default value is blank in both cases. Can you please suggest?

Approach 1

class tblusers(models.Model):
    created_at = DateTimeField(default = datetime.now)
    #trying to set default value = now
    is_active = BooleanField(default = 0, null = True)
    #trying to set default value = 0

Approach 2

class tblusers(models.Model):
    created_at = DateTimeField(default = timezone.now)
    #trying to set default value = now
    is_active = BooleanField(default = 0, null = True)
    #trying to set default value = 0
Abcdef
  • 43
  • 7

1 Answers1

0

I typically do this: created_at = models.DateTimeField(auto_now_add=True)

Did you see this question? How to set a Django model field's default value to a function call / callable (e.g., a date relative to the time of model object creation)

iragm
  • 51
  • 4
  • 0001_initial.py has this code `('created_at', models.DateTimeField(auto_now_add=True)),` after changing my code as you suggested and then in the mysql database side, created_at has value None. – Abcdef Sep 21 '21 at 12:08
  • 1
    Without `null=True`, it can't have value `None`. – aaron Sep 26 '21 at 06:29