17

Django is proving the model field argument default (https://docs.djangoproject.com/en/dev/ref/models/fields/#default) but as I know it will be called every time new object is created via django. If we insert/create a records with raw queries (using django.db.connection.cursor) we will get the exception cause Field 'xyz' doesn't have a default value.

How to represent the db level default value for the column in model. Like db_index.

I hope you guy understand my question.

1
  • As you wrote, default value is set by default argument in model field. You need to set default value in raw query? Commented Sep 7, 2014 at 19:01

1 Answer 1

14

There is an open ticket 470 to include default values in the SQL schema.

Until this feature has been added to Django, you'll have to manually run alter table statements yourself or write a migration to run them if you want a default value in the SQL schema.

Note that Django allows callables as defaults, so even if this feature is added to Django, it won't be possible to have all defaults in the database.

Sign up to request clarification or add additional context in comments.

4 Comments

Ticket 470 was opened September 2005, and marked WONTFIX shortly after, due to cross-database quoting concerns. So, do not wait for this feature!
@jwhitlock the ticket was reopened so it might appear eventually. That was 4 years ago so don’t hold your breath!
ticket 470 has been finally closed, the pull request is merged but the code is not yet released in any official Django release. If someone is waiting for that ticket - it's getting close
Field.db_default coming in Django 5.0 it would seem

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.