1

I have the following model at Django:

class Community(models.Model):

    name = models.CharField(max_length=255)
    members = models.ManyToManyField(User, through='Membership')
    date_created = models.DateTimeField(auto_now_add=True)

But when I check the structure of the table (using Postico for PostgreSQL) the field of date_created after applying the migrations shows no default.

enter image description here

I have also tried with explicitly default=date.today() but it does not work.

Any ideas what I am missing?

Thanks,
Pablo

EDIT

Great thanks to this post: How to make a script to insert data in my default sqlite3 database django

I was trying to populate the database via script using PostgreSQL driver, when it is way simpler importing the Django models a use the create method (also thanks to Daniel Roseman in the comments that led me find the post).

3
  • 3
    The default attributes are typically not set in the database later, but in the Django layer, since you can attach anything function to it. For example functions that depend on the time, Django settings, etc. Commented May 14, 2018 at 16:58
  • What about if I am making a script to populate the table with random generated communities using psycopg2? It is demanding the date_created value Commented May 14, 2018 at 17:00
  • You should use Django's model classes to write your script, rather than direct db access. Commented May 14, 2018 at 18:28

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.